how to compare two json file line by line using python?

Avnesh Shakya avnesh.nitk at gmail.com
Mon May 27 01:05:39 EDT 2013


Actually, I am extracting data from other site in json format and I want to
put it in my database and when I extract data again then I want to compare
last json file, if these are same then no issue otherwise i will add new
data in database, so here may be every time data can be changed or may be
not so I think sorting is required, but if i compare line by line that will
be good, I am thinking in this way...


On Mon, May 27, 2013 at 10:21 AM, rusi <rustompmody at gmail.com> wrote:

> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130527/5424bda9/attachment.html>


More information about the Python-list mailing list