Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare 2 json in python [closed]

Tags:

python

How to compare 2 json objects in python below are the sample json.

sample_json1={
    {
       "globalControlId": 72,
       "value": 0,
       "controlId": 2
   },
   {
       "globalControlId": 77,
       "value": 3,
       "controlId": 7
   }
}

sample_json2={
    {
       "globalControlId": 72,
       "value": 0,
       "controlId": 2
   },
   {
       "globalControlId": 77,
       "value": 3,
       "controlId": 7
   }
}
like image 250
santosh Avatar asked Jun 21 '12 15:06

santosh


People also ask

How do I compare two JSON files in Python?

Comparing Json: Comparing json is quite simple, we can use '==' operator, Note: '==' and 'is' operator are not same, '==' operator is use to check equality of values , whereas 'is' operator is used to check reference equality, hence one should use '==' operator, 'is' operator will not give expected result.

How do I compare two JSON files?

You can also directly compare two JSON files by specifying their urls in the GET parameters url1 and url2. Then you can visualize the differences between the two JSON documents. It highlights the elements which are different: Different value between the two JSON: highlight in red color.

How do I analyze a JSON file in Python?

Python has a built in module that allows you to work with JSON data. At the top of your file, you will need to import the json module. If you need to parse a JSON string that returns a dictionary, then you can use the json. loads() method.


1 Answers

It seems that the usual comparison working properly

import json
x = json.loads("""[
    {
       "globalControlId": 72,
       "value": 0,
       "controlId": 2
   },
   {
       "globalControlId": 77,
       "value": 3,
       "controlId": 7
   }
]""")

y = json.loads("""[{"value": 0, "globalControlId": 72,"controlId": 2}, {"globalControlId": 77, "value": 3, "controlId": 7 }]""")

x == y # result: True    
like image 64
defuz Avatar answered Oct 21 '22 08:10

defuz