user29496
user29496

Reputation: 319

Compare difference in two json files and out put the difference

I am trying to compare difference between two json files and output the list of r_id values which are present in file a but not in file b.

Json files which i am trying to compare

File a =

   {“r_id”:”123”,"RefNumber”:”2341234131","amount":"22.99”},
   {“r_id”:”345”,"RefNumber”:”2341234131","amount":"22.99”},
   {“r_id”:”678”,"RefNumber”:”2341234131","amount":"22.99”}

File b =

     {“name” : “James”, "id" : “123”, “class” : “1A”},
     {“name” : “Sam”,"id" : “345”, “class” : “1A”},
     {“name” : “Jen”,"id" : “005”, “class” : “1A”}

Comparison should be based on id's in both files. Expecting following output in difference file

{“r_id”:”678”,"RefNumber”:”2341234131","amount":"22.99”}

Upvotes: 0

Views: 1228

Answers (2)

andreis11
andreis11

Reputation: 1141

This will work if ids are not in order and jsons don't have equal items.

import json

with open("json_a.json","r") as first, open("json_b.json","r") as second :
  b =  json.load(first,object_pairs_hook=lambda x: x[0])
  c =  json.load(second,object_pairs_hook=lambda x: x[1])

b = [ _[1] for _ in b]
c = [ _[1] for _ in c]

with open("json_a.json","r") as first:
  for each_line in json.load(first):
    for uniq_id in list(set(b).difference(c)):
      if each_line['r_id']== uniq_id :
        print(each_line)

Another approach:

import json

with open("json_a.json","r") as first, open("json_b.json","r") as second :
  b =  json.load(first)
  c =  json.load(second)

b_ids=[x['r_id'] for x in b]

c_ids=[x['id'] for x in c]

for each_item in b:
  for uniq_id in list(set(b_ids).difference(c_ids)):
    if each_item['r_id'] == uniq_id:
      print(each_item)

Write to file:

# Serializing json
json_object = json.dumps(each_item)

# Writing to sample.json 
with open("sample.json", "w") as outfile: 
    outfile.write(json_object)

More details about file writing options can be found here.

Upvotes: 1

Saragada Ramesh
Saragada Ramesh

Reputation: 201

Try this Code :

import json

a = ['{"r_id":"123","RefNumber":"2341234131","amount":"22.99"}',
     '{"r_id":"345","RefNumber":"2341234131","amount":"22.99"}',
     '{"r_id":"678","RefNumber":"2341234131","amount":"22.99"}'
   ]


b = [ '{"name" : "James", "id" : "123", "class" : "1A"}',
      '{"name" : "Sam", "id" : "345", "class" : "1A"}',
      '{"name" : "Jen", "id" : "005", "class" : "1A"}'
     ]


for i in range(len(a)):
    y = json.loads(a[i])
    z = json.loads(b[i])
    if y["r_id"] != z["id"]:
        print(a[i])

Output :

{"r_id":"678","RefNumber":"2341234131","amount":"22.99"}

Before working with json files the file should be like below format :

[{"r_id":"123","RefNumber":"2341234131","amount":"22.99"},
 {"r_id":"345","RefNumber":"2341234131","amount":"22.99"},
 {"r_id":"678","RefNumber":"2341234131","amount":"22.99"}
]

Try with this code(with files):

import json

with open('file1.json','r') as a:
    data1 = a.read()
obj1 = json.loads(data1)

with open('file2.json','r') as a:
    data2 = a.read()
obj2 = json.loads(data2)

count = 0
for i in obj1:
    a = obj2[count]
    if i["r_id"] != a["id"]:
        print(i)
    count = count + 1

Output is same as above.

Upvotes: 1

Related Questions