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

Avnesh Shakya avnesh.nitk at gmail.com
Mon May 27 02:28:42 EDT 2013


Thanks a lot, I got it.


On Mon, May 27, 2013 at 11:03 AM, Steven D'Aprano <
steve+comp.lang.python at pearwood.info> wrote:

> On Sun, 26 May 2013 21:32:40 -0700, Avnesh Shakya wrote:
>
> > 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.
>
> Why do you care about checking every value line by line? As you say
> yourself, JSON data is unordered, so "line by line" is the wrong way to
> compare it.
>
>
> The right way is to decode the JSON data, and then compare whether it
> gives you the result you expect:
>
> a = json.load("file-a")
> b = json.load("file-b")
> if a == b:
>     print("file-a and file-b contain the same JSON data")
>
> If what you care about is the *data* stored in the JSON file, this is the
> correct way to check it.
>
> On the other hand, if you don't care about the data, but you want to
> detect changes to whitespace, blank lines, or other changes that make no
> difference to the JSON data, then there is no need to care that this is
> JSON data. Just treat it as text, and use the difflib library.
>
> http://docs.python.org/2/library/difflib.html
>
>
> --
> Steven
> --
> 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/e5d0a512/attachment.html>


More information about the Python-list mailing list