Compare two JSON objects (Python) (2024)

In this short article, we will see quick and easiest way to perform comparison on Json object in python:

Compare two JSON objects (Python) (2)
  1. Comparing two json object, return ‘True’ if both json are same otherwise
  2. ‘False’
  3. Edge case of comparing json objects using “==”
  4. If two json are not equal then find the exact difference.

Comparing json is quite simple, we can use ‘==’ operator,

Compare two JSON objects (Python) (3)
Compare two JSON objects (Python) (4)

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.

Comparing two dictionaries has been solved in the first part of this articles. Now let’s image we have the following dicts to compare :

Dict 1 :

{
"errors": [
{"error": "invalid", "field": "email"},
{"error": "required", "field": "name"}
],
"success": false
}

Dict 2 :

{
"success": false,
"errors": [
{"error": "required", "field": "name"},
{"error": "invalid", "field": "email"}
]
}
>>> dict1 == dict2
False

let’s decode them and compare. Order does not matter for dictionary as long as the keys, and values matches. (Dictionary has no order in Python)

>>> {'a': 1, 'b': 2} == {'b': 2, 'a': 1}
True

But order is important in list; sorting will solve the problem for the lists.

>>> [1, 2] == [2, 1]
False
>>> [1, 2] == sorted([2, 1])
True
>>> a = '{"errors": [{"error": "invalid", "field": "email"}, {"error": "required", "field": "name"}], "success": false}'>>> b = '{"errors": [{"error": "required", "field": "name"}, {"error": "invalid", "field": "email"}], "success": false}'>>> a, b = json.loads(a), json.loads(b)
>>> a['errors'].sort()
>>> b['errors'].sort()
>>> a == b
True

Above example will work for the JSON in the question.

Finding exact difference in two json sounds difficult task, it may become even more difficult, if we try to find differences in nested jsons. Programmatically, one can write a small piece of code which would iterate every keys of json and pick the differences, but this work will become very difficult if we don’t know how nested the json is. But, we don’t really have to worry of writing code and all, This is where deepdiff comes in handy. Deepdiff is a powerful python library to compare 2 dictionaries. What makes it powerful is that, during the comparison, deepdiff does not consider the order in which the elements inside the dictionaries are present.
Let’s see deepdiff in action :

1.Elements newly added.

2. Elements removed.

3. Elements whose values changed.

Consider below example, jsn_1 contains three items with keys ‘a’,’b’,’c’ respectively, in jsn_2 below changes has been done:

a. Element ‘c’ is removed.

b. Element ‘e’ and ‘d’ added.

c. Value of key ‘b’ has changed.

Compare two JSON objects (Python) (5)

DeepDiff function of deepdiff module returns all the changes, let's find all differences using deepdiff:

Compare two JSON objects (Python) (6)
Compare two JSON objects (Python) (7)

Elements Added:

Compare two JSON objects (Python) (8)

Elements Removed:

Compare two JSON objects (Python) (9)

Values changed:

Compare two JSON objects (Python) (10)

Conclusion:

  1. We have seen easiest way to compare and find the differences in json objects.
  2. ‘==’ is used for comparing json.
  3. Dictionary has no order in Python but order is important in list
  4. DeepDiff function of deepdiff library can be leveraged to find differences.
Compare two JSON objects (Python) (2024)
Top Articles
How To Use the 40-30-20-10 Rule To Boost Your Savings
Benefits of Video Games For Kids & Adults
Davita Internet
Amc Near My Location
Brady Hughes Justified
Lifewitceee
Belle Meade Barbershop | Uncle Classic Barbershop | Nashville Barbers
Blackstone Launchpad Ucf
Rondale Moore Or Gabe Davis
50 Meowbahh Fun Facts: Net Worth, Age, Birthday, Face Reveal, YouTube Earnings, Girlfriend, Doxxed, Discord, Fanart, TikTok, Instagram, Etc
Tap Tap Run Coupon Codes
Osrs But Damage
Fcs Teamehub
Heska Ulite
Obituary | Shawn Alexander | Russell Funeral Home, Inc.
What Is A Good Estimate For 380 Of 60
Bros Movie Wiki
Evil Dead Rise Showtimes Near Regal Columbiana Grande
Is Windbound Multiplayer
Soulstone Survivors Igg
Tips and Walkthrough: Candy Crush Level 9795
UMvC3 OTT: Welcome to 2013!
Wat is een hickmann?
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Mchoul Funeral Home Of Fishkill Inc. Services
Elanco Rebates.com 2022
Robert A McDougal: XPP Tutorial
Kamzz Llc
Issue Monday, September 23, 2024
Miss America Voy Board
Watchdocumentaries Gun Mayhem 2
Lichen - 1.17.0 - Gemsbok! Antler Windchimes! Shoji Screens!
Radical Red Doc
Baywatch 2017 123Movies
Improving curriculum alignment and achieving learning goals by making the curriculum visible | Semantic Scholar
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Craigslist Com St Cloud Mn
Swoop Amazon S3
Joblink Maine
Noga Funeral Home Obituaries
Phone Store On 91St Brown Deer
Displacer Cub – 5th Edition SRD
4Chan Zelda Totk
Naomi Soraya Zelda
Bradshaw And Range Obituaries
St Als Elm Clinic
A Snowy Day In Oakland Showtimes Near Maya Pittsburg Cinemas
Fredatmcd.read.inkling.com
Law Students
Cataz.net Android Movies Apk
Lux Nails & Spa
Emmi-Sellers
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6270

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.