Back to Python
2025-12-256 min read

JSON Compare (Python Programming)

Learn JSON Compare (Python Programming) step by step with clear examples and exercises.

Title: JSON Compare (Python Programming)

Why This Matters

JSON (JavaScript Object Notation) is an essential data format with wide usage for data interchange between applications. Comparing two JSON files can help identify differences, ensure consistency, and debug issues in your code. In this lesson, we will learn how to compare two JSON files using Python, which is crucial for developers working on projects that involve data management and integration.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of the following concepts:

  • Python programming basics (variables, functions, loops, conditional statements)
  • JSON format and handling in Python (loading, parsing, manipulating)
  • Familiarity with data structures like lists and dictionaries in Python

Additional Resources

To brush up on your Python skills, check out the following resources:

  1. Python Data Structures

Core Concept

Python provides multiple libraries to work with JSON data. In this lesson, we will use the built-in json library for comparing two JSON files. The json module allows us to load, parse, and manipulate JSON data in Python.

To compare two JSON files, we can follow these steps:

  1. Load both JSON files as Python dictionaries using the json.load() function.
  2. Convert the dictionaries into lists of keys (for easy comparison).
  3. Compare the lists to find differences between the JSON files.
  4. Iterate through common keys and compare their values.

Here's a simple example of how to compare two JSON files:

import json

Load the first JSON file

with open('file1.json') as f1:

data1 = json.load(f1)

Load the second JSON file

with open('file2.json') as f2:

data2 = json.load(f2)

Convert both dictionaries into lists of keys

keys_list1 = list(data1.keys())

keys_list2 = list(data2.keys())

Find differences between the two lists (using set difference)

differences = set(keys_list1).symmetric_difference(set(keys_list2))

Iterate through common keys and compare their values

for key in set(keys_list1) & set(keys_list2):

if data1[key] != data2[key]:

print(f"Different value for key '{key}':")

print(f"File 1: {data1[key]}")

print(f"File 2: {data2[key]}")

Print any keys that exist only in one file

for key in differences:

if key in data1 and key not in data2:

print(f"Key '{key}' exists only in File 1.")

elif key in data2 and key not in data1:

print(f"Key '{key}' exists only in File 2.")


In this example, we first load both JSON files as Python dictionaries. We then convert the dictionaries into lists of keys for easy comparison. The `symmetric_difference()` method is used to find the elements that are in either list but not in both (i.e., differences between the two lists).

We iterate through common keys and compare their values to identify any discrepancies. Additionally, we print out any keys that exist only in one file.

Worked Example

Let's compare two JSON files containing data about employees:

file1.json

{
"employee1": {
"name": "John Doe",
"age": 30,
"salary": 50000
},
"employee2": {
"name": "Jane Smith",
"age": 28,
"salary": 45000
}
}

file2.json

{
"employee1": {
"name": "John Doe",
"age": 30,
"salary": 50000,
"department": "IT"
},
"employee3": {
"name": "Alice Johnson",
"age": 26,
"salary": 48000
}
}

Here's the Python code to compare these two JSON files:

import json

Load the first JSON file

with open('file1.json') as f1:

data1 = json.load(f1)

Load the second JSON file

with open('file2.json') as f2:

data2 = json.load(f2)

Convert both dictionaries into lists of keys

keys_list1 = list(data1.keys())

keys_list2 = list(data2.keys())

Find differences between the two lists (using set difference)

differences = set(keys_list1).symmetric_difference(set(keys_list2))

Iterate through common keys and compare their values

for key in set(keys_list1) & set(keys_list2):

if data1[key] != data2[key]:

print(f"Different value for key '{key}':")

print(f"File 1: {data1[key]}")

print(f"File 2: {data2[key]}")

Print any keys that exist only in one file

for key in differences:

if key in data1 and key not in data2:

print(f"Key '{key}' exists only in File 1.")

elif key in data2 and key not in data1:

print(f"Key '{key}' exists only in File 2.")


When you run this code, it will output:

Different value for key 'department':

File 1: None

File 2: IT

Key 'employee3' exists only in File 2.


This indicates that the `employee3` key is present in the second JSON file but not in the first one, and the `department` key has a different value in both files.

Common Mistakes

  1. Forgetting to load both JSON files: Make sure you load both files using the json.load() function before comparing them.
  2. Not converting dictionaries into lists of keys: Converting the dictionaries into lists of keys is essential for easy comparison.
  3. Using the wrong method to find differences: Instead of symmetric_difference(), you might use other methods like difference() or intersection(). However, these methods will not give you the correct result when comparing JSON files.
  4. Not handling duplicate keys: If both JSON files have duplicate keys with different values, the comparison might not yield the expected results. Make sure to handle such cases appropriately.
  5. Not considering the order of keys: The order of keys in a JSON file is not guaranteed to be consistent. When comparing two JSON files, you should either ignore key order or sort both lists before finding differences.
  6. Ignoring value types: Ensure that you consider the data types (e.g., string, integer, float) of values when comparing them.
  7. Not handling missing keys: If a key exists in one file but not the other, handle this situation appropriately (e.g., setting a default value or raising an error).

Subheadings under Common Mistakes:

  • Handling Missing Keys
  • Considering Value Types

Practice Questions

  1. Write Python code to compare two JSON files and print out the differences in values for common keys.
  2. Modify the example provided earlier to handle duplicate keys with different values.
  3. Write a function that takes two JSON files as input, compares them, and returns a report detailing the differences found.
  4. Given two JSON files containing data about products, write Python code to compare the product prices and print out the products with different prices.
  5. Write a script that reads multiple JSON files in a directory and generates a summary of the differences found across all files.

FAQ

  1. Why not use other libraries like jsonschema or jsonpatch for comparing JSON files?

While there are other libraries available for comparing JSON files, using the built-in json library is a simple and efficient approach when you only need to find differences between two files. However, if your requirements include validating JSON against a schema or applying patches to a JSON file, you should consider using specialized libraries like jsonschema or jsonpatch.

  1. What happens if the JSON files have different indentation or formatting?

The built-in json library in Python ignores differences in indentation and formatting when loading JSON data. This means that you can compare two JSON files with different formats without worrying about these differences affecting the comparison result.

  1. Can I compare multiple JSON files at once using a single Python script?

Yes, you can modify the example provided earlier to accept a list of JSON file paths and output differences between all pairs of files. This will help you compare multiple JSON files efficiently in one go.

  1. How can I handle missing keys when comparing JSON files?

When comparing JSON files, consider handling missing keys appropriately. You can set a default value for missing keys or raise an error to indicate that there is a difference between the files.

  1. What if I want to compare JSON data with different data types (e.g., string vs integer)?

Ensure that you consider the data types of values when comparing them. You can convert all values to a common data type (e.g., string) before comparison or handle differences based on the specific requirements of your project.

JSON Compare (Python Programming) | Python | XQA Learn