<div dir="ltr"><div><br></div>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...<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, May 27, 2013 at 10:21 AM, rusi <span dir="ltr"><<a href="mailto:rustompmody@gmail.com" target="_blank">rustompmody@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On May 27, 9:32 am, Avnesh Shakya <<a href="mailto:avnesh.n...@gmail.com">avnesh.n...@gmail.com</a>> wrote:<br>
> hi,<br>
> how to compare two json file line by line using python? Actually I am doing it in this way..<br>
><br>
> import simplejson as json<br>
> def compare():<br>
> newJsonFile= open('newData.json')<br>
> lastJsonFile= open('version1.json')<br>
> newLines = newJsonFile.readlines()<br>
> print newLines<br>
> sortedNew = sorted([repr(x) for x in newJsonFile])<br>
> sortedLast = sorted([repr(x) for x in lastJsonFile])<br>
> print(sortedNew == sortedLast)<br>
><br>
> compare()<br>
><br>
> 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.<br>
> I want to check every value line by line.<br>
><br>
> Thanks<br>
<br>
</div></div>It really depends on what is your notion that the two files are same<br>
or not.<br>
<br>
For example does extra/deleted non-significant white-space matter?<br>
<br>
By and large there are two approaches:<br>
1. Treat json as serialized python data-structures, (and so) read in<br>
the data-structures into python and compare there<br>
<br>
2. Ignore the fact that the json file is a json file; just treat it as<br>
text and use string compare operations<br>
<br>
Naturally there could be other considerations: the files could be huge<br>
and so you might want some hybrid of json and text approaches<br>
etc etc<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div>