I would like to compare two json values like
source1 =
{ "a" : "value 1",
"b" : "value to be updated",
"c" : "value 3",
"d" : "second value to be updated"
}
target = {
"a" : "value 1",
"b" : "value 2"",
"c" : "value 3",
"d" : "second 4"}
Now , I would like to compare both the json which will have same key but values may differ . How can I find the differed value?
My end result should look like :
{
"a" : "value 1"
"b" : "value 2"
"c" : "value 3"
"d" : "second4"
}
In the code the first set of key:value needs to be compared with second set and if the second set contains different values , then update the key:value and create a third json set :
import json
source = '{"a" : "value 1","b" : "value to be updated","c" : "value 3","d" : "second value to be updated" }'
target = '{"a": "value1" , "b" : 2 , "c" : "value 3" , "d" : 4}'
jsonload = (json.loads(source))
jsonload1 = (json.loads(target))
for k in jsonload:
for k in jsonload1:
if jsonload[k] != jsonload1[k]:
print("key of" , k ,"not matched in ", k ,jsonload[k],jsonload1[k])
output :
('key of', u'a', 'not matched in ', u'a', u'value 1', u'value1') ('key of', u'b', 'not matched in ', u'b', u'value to be updated', 2) ('key of', u'd', 'not matched in ', u'd', u'second value to be updated', 4) ('key of', u'a', 'not matched in ', u'a', u'value 1', u'value1') ('key of', u'b', 'not matched in ', u'b', u'value to be updated', 2) ('key of', u'd', 'not matched in ', u'd', u'second value to be updated', 4) ('key of', u'a', 'not matched in ', u'a', u'value 1', u'value1') ('key of', u'b', 'not matched in ', u'b', u'value to be updated', 2) ('key of', u'd', 'not matched in ', u'd', u'second value to be updated', 4) ('key of', u'a', 'not matched in ', u'a', u'value 1', u'value1') ('key of', u'b', 'not matched in ', u'b', u'value to be updated', 2) ('key of', u'd', 'not matched in ', u'd', u'second value to be updated', 4)