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
This is why you should start utilizing USDT payments – Triple-A
Is Paying Off HECS Debt Early a Wise Choice? – Inspired Money
Frederick County Craigslist
Garrison Blacksmith Bench
Western Union Mexico Rate
Www.metaquest/Device Code
Wmu Course Offerings
Chase Claypool Pfr
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
What is IXL and How Does it Work?
Mlb Ballpark Pal
Housework 2 Jab
Nebraska Furniture Tables
Tcgplayer Store
Skyward Login Jennings County
Zoe Mintz Adam Duritz
Milanka Kudel Telegram
Xsensual Portland
SuperPay.Me Review 2023 | Legitimate and user-friendly
Doublelist Paducah Ky
Shreveport City Warrants Lookup
6892697335
Enduring Word John 15
John Deere 44 Snowblower Parts Manual
Play It Again Sports Forsyth Photos
Pipa Mountain Hot Pot渝味晓宇重庆老火锅 Menu
Dreamcargiveaways
Strange World Showtimes Near Atlas Cinemas Great Lakes Stadium 16
Maybe Meant To Be Chapter 43
Naya Padkar Newspaper Today
To Give A Guarantee Promise Figgerits
Radical Red Doc
Empire Visionworks The Crossings Clifton Park Photos
Craigslist Boats Eugene Oregon
Bitchinbubba Face
Pepsi Collaboration
Joey Gentile Lpsg
craigslist: modesto jobs, apartments, for sale, services, community, and events
Shane Gillis’s Fall and Rise
Author's Purpose And Viewpoint In The Dark Game Part 3
Barstool Sports Gif
Sand Castle Parents Guide
Mcalister's Deli Warrington Reviews
Courses In Touch
Gamestop Store Manager Pay
Jaefeetz
Florida Lottery Powerball Double Play
Tlc Africa Deaths 2021
Take Me To The Closest Ups
Guy Ritchie's The Covenant Showtimes Near Look Cinemas Redlands
How to Get a Check Stub From Money Network
Lagrone Funeral Chapel & Crematory Obituaries
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.