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

A

Avnesh Shakya

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
 
R

rusi

hi,
   how to compare two json file line by line using python? Actually Iam 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
 
A

Avnesh Shakya

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...


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
 
S

Steven D'Aprano

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
 
D

Denis McMahon

how to compare two json file line by line using python? Actually I am
doing it in this way..

Oh what a lot of homework you have today.

Did you ever stop to think what the easiest way to compare two json
datasets is?
 
G

Grant Edwards

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.

There's no such thing as "lines" in JSON anyway. Outside of string
literals, all whitespace is equivalent, so replacing all newlines with
space characters results in equivalent blobs of JSON -- but one is a
single line, and the other is multiple lines.
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.

Yup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top