how to compare two json file line by line using python?
rusi
rustompmody at gmail.com
Mon May 27 00:51:59 EDT 2013
On May 27, 9:32 am, Avnesh Shakya <avnesh.n... at gmail.com> wrote:
> hi,
> how to compare two json file line by line using python? Actually I am doing it in this way..
>
> import simplejson as json
> def compare():
> newJsonFile= open('newData.json')
> lastJsonFile= open('version1.json')
> newLines = newJsonFile.readlines()
> print newLines
> sortedNew = sorted([repr(x) for x in newJsonFile])
> sortedLast = sorted([repr(x) for x in lastJsonFile])
> print(sortedNew == sortedLast)
>
> compare()
>
> But I want to compare line by line and value by value. but i found that json data is unordered data, so how can i compare them without sorting it. please give me some idea about it. I am new for it.
> I want to check every value line by line.
>
> Thanks
It really depends on what is your notion that the two files are same
or not.
For example does extra/deleted non-significant white-space matter?
By and large there are two approaches:
1. Treat json as serialized python data-structures, (and so) read in
the data-structures into python and compare there
2. Ignore the fact that the json file is a json file; just treat it as
text and use string compare operations
Naturally there could be other considerations: the files could be huge
and so you might want some hybrid of json and text approaches
etc etc
More information about the Python-list
mailing list