JSONdiff in Python: Streamlining JSON Data Comparisons

You might have come across the jsondiff Python library that lets you draw a comparison among JSON files. This article describes the jsondiff library in detail.

Installation of jsondiff

In order to work with the jsondiff library, you should install it with the pip command first. The following command shows how you can install this library. After installation, you have to import this library.

pip install jsondiff
import jsondiff 

Functions present in jsondiff

The jsondiff library consists of the following functions:

  • compare()
  • diff()
  • patch()
  • symbols()

These functions are centered on different aspects. The compare() function generates a comparison report when it compares two JSON objects. The diff() function also does the same. patch() transforms one object to another. Also, symbols() show the differences between two JSON objects in the form of symbols.

The operations which are used with patch() are:

Example of jsondiff

Let’s have a look at this example, which has two JSON objects. The compare() function works on assessing both the objects and draws a relation between them.

import jsondiff

# Load the JSON objects to compare
json1 = json.loads('{"name": "Alice", "age": 25}')
json2 = json.loads('{"name": "Bob", "age": 30}')

# Compare the JSON objects
diff = jsondiff.compare(json1, json2)

# Print the diff report
print(jsondiff.dumps(diff))

The output is in this form:

{
  "added": {
    "name": "Bob"
  },
  "changed": {
    "age": 30
  }
}

This basically means that the dump function enlists the differences that exist between these two objects. It displays output with two properties:

  • added
  • changed

The first property implies a new addition to the second object, i.e., something that was not previously present. The second property, called changed, means the keys that are common to both objects but were changed in the second object.

More about dump in jsondiff

The dumps() function accepts a diff report and returns the answer in the form of JSON file differences. Thus, its return type is a JSON string that consists of the differences.

import jsondiff
import json

diff_report = {
  "added": {
    "new_key": "new_value"
  },
  "changed": {
    "existing_key": "new_value"
  },
  "removed": {
    "old_key": "old_value"
  }
}

json_string = jsondiff.dumps(diff_report)
print(json_string)

Now let us check the output:

{
  "added": {
    "new_key": "new_value"
  },
  "changed": {
    "existing_key": "new_value"
  },
  "removed": {
    "old_key": "old_value"
  }
}

You may use the dump and specify the type of formatting you want. You can indent the output or arrange it in a sorted manner. Let us have a look at the following example:

import jsondiff
import json

diff_report = {
  "added": {
    "new_key": "new_value"
  },
  "changed": {
    "existing_key": "new_value"
  },
  "removed": {
    "old_key": "old_value"
  }
}

json_string = jsondiff.dumps(diff_report, indent=4, sort_keys=True)
print(json_string)

Now, see the output differs from the previous one.

{
  "added": {
    "new_key": "new_value"
  },
  "changed": {
    "existing_key": "new_value"
  },
  "removed": {
    "old_key": "old_value"
  }
}

Other uses of jsondiff

  • Exclude keys from the comparison
  • Compare only specific keys
  • Compare JSON objects that are stored in files
  • Generate a diff report in HTML format.

Multilevel files with jsondiff

Multilevel JSON files have :

  • nested structures or
  • hierarchical relationships

Users can consider working on multilevel JSON files with the help of the jsondiff library. They are used with configuration files, API responses, or data models.

Working with jsondiff

You need to install and import jsondiff first. After that, you should load your files with the open() command. You can then use the compare function. This produces a diff report. Lastly, you should print the diff report.

pip install jsondiff
import jsondiff
with open("file1.json", "r") as f:
    json1 = json.load(f)
with open("file2.json", "r") as f:
    json2 = json.load(f)
diff = jsondiff.compare(json1, json2)
print(jsondiff.dumps(diff))

jsondiff with files and functions

jsondiff library is commonly used with functions. The method of loading the files is the same. Properties like added, changed and removed notify us of the changes in the two files. The function call statement had the two files as its parameters.

import jsondiff
import json

def compare_json_files(json1_path, json2_path):
    with open(json1_path, "r") as f:
        json1 = json.load(f)

    with open(json2_path, "r") as f:
        json2 = json.load(f)

    diff = jsondiff.compare(json1, json2)

    return diff

if __name__ == "__main__":
    json1_path = "file1.json"
    json2_path = "file2.json"

    diff = compare_json_files(json1_path, json2_path)

    print(jsondiff.dumps(diff))

jsondiff for validating API response

This Python library can help one validate an API response, too. It compares the expected API response to the actual one. This ensures consistency in API calls, and it is checked whether these calls follow API documentation. In this case, the jsondiff library can work with the requests library for the following tasks:

  • Presence of expected keys
  • Validate the values of keys
  • Check the structure of nested objects and arrays.
  • Look for changes in API responses.

Therefore, this library proves to be fruitful in this regard. The following example shows how you can use the jsondiff library along with the requests library in order to document API responses.

import jsondiff
import requests

# Make the API call
response = requests.get('https://api.example.com/data')

# Convert the response to JSON
json_response = json.loads(response.content)

# Load the expected API response
with open('expected_response.json', 'r') as f:
    expected_response = json.load(f)

# Compare the actual and expected responses
diff = jsondiff.compare(json_response, expected_response)

# Print the diff report
print(jsondiff.dumps(diff))

If not even a single difference exists, it means the diff report is empty, and the API response matches the actual one. If there are a few differences, we need to make the call again and recheck our response.

Arrays with jsondiff

The jsondiff library can also work with arrays to spot the differences and produce results in JSON FORMAT. Your input should be an array, while the output format will be similar to what the jsondiff library produces normally.

import jsondiff

# Load the JSON arrays to compare
json1 = json.loads('[1, 2, 3]')
json2 = json.loads('[2, 3]')

# Compare the JSON arrays
diff = jsondiff.compare(json1, json2)

# Print the diff report
print(jsondiff.dumps(diff))

Now, understand the output. It shows that the element at index 0 no longer exists.

{
  "removed": {
    0: 1
  }
}

Nested objects with jsondiff

jsondiff lets you work with nested objects as well. The following example illustrates the same.

import jsondiff

# Load the JSON objects to compare
json1 = json.loads('{"name": "Alice", "age": 25, "address": {"street": "123 Main St", "city": "Anytown"}}')
json2 = json.loads('{"name": "Bob", "age": 30, "address": {"street": "456 Elm St", "city": "Anytown"}}')

# Compare the JSON objects
diff = jsondiff.compare(json1, json2)

# Print the diff report
print(jsondiff.dumps(diff))

Now, when you go through the output, you will know that it is functioning the same way. As per this example, name, age, and address. Streets have undergone changes.

{
  "changed": {
    "name": "Bob",
    "age": 30,
    "address": {
      "street": "456 Elm St"
    }
  }
}

deepdiff vs jsondiff

Let us see how the deepdiff library is different from the jsondiff library. It works on the following complex data structures:

  • dictionaries,
  • lists,
  • strings, and
  • custom objects
FeatureDeepDiffIt is centered primarily on JSON objects.
FunctionalityJSON diff provides basic comparison options only.It performs a deep comparison of any object
FeaturesIt offers extensive customization optionsIt makes simple comparisons of JSON objects where detailed reports are not essential
OutputIt provides a detailed report of all changesThis library gives a concise overview of changes
Best forIt makes complex comparisons with diverse data structures and customized requirements.It makes simple comparisons of JSON objects where detailed reports are not essential.

Limitations of JSON diff python

Even though JSON diff python is an easy-to-use library, it has a few limitations like limited data types and disorganized diff format. It can support strings, numbers, boolean, and arrays but is not quite flexible with complex structures. The same goes for super nested objects. The format of the Diff report is not standardized. This way, the report generated by different tools will provide non-identical results. This will be difficult to compare.

FAQs

What are the limitations of jsondiff python?

Limited data types and disorganized diff format are two limitations of jsondiff python.

When should I use DeepDiff vs. other libraries like jsondiff?

In case you want to do complex comparisons with complex data structures, go with deepdiff. Otherwise, you can keep using jsondiff only.

Conclusion

This blog covers an introduction to the jsondiff library in Python. It’s an extremely informative yet simple library to compare objects in the JSON format.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments