Made by Carbon.now.sh

Debugging with DeepDiff: Deep Differences in Python JSON, Dicts, & Objects

Jerry
3 min readApr 22, 2023

--

Debugging complex data structures is a common task in many Python projects, and can often be a time-consuming and frustrating process. Fortunately, the DeepDiff Python library provides a powerful set of tools for comparing and analyzing complex data structures, making it much easier to debug and troubleshoot Python code. In this tutorial, we will explore some practical examples of using DeepDiff to simplify the debugging process.

Installing DeepDiff:

To get started with DeepDiff, you will need to install it using pip. You can do this by running the following command in your terminal:

pip install deepdiff

Once you have installed DeepDiff, you can start using it in your Python projects.

Example 1: Comparing JSON Objects

One common use case for DeepDiff is comparing JSON objects. JSON is a widely used format for storing and exchanging data, and is often used in web applications and APIs. Let’s say you have two JSON objects that you want to compare:

import json

from deepdiff import DeepDiff
json1 = '{"name": "John", "age": 30, "city": "New York"}'
json2 = '{"name": "Jane", "age": 25, "city": "San Francisco"}'
dict1 = json.loads(json1)
dict2 = json.loads(json2)
diff = DeepDiff(dict1, dict2)
print(diff)

This will output:

{‘values_changed’: {“root[‘name’]”: {‘new_value’: ‘Jane’, ‘old_value’: ‘John’}, “root[‘age’]”: {‘new_value’: 25, ‘old_value’: 30}}, ‘dictionary_item_added’: {“root[‘city’]”: ‘San Francisco’}, ‘dictionary_item_removed’: {“root[‘city’]”: ‘New York’}}

As we can see, DeepDiff has identified the differences between the two JSON objects, including changes in values and added or removed dictionary items.

Example 2: Comparing Database Records

Another common use case for DeepDiff is comparing database records. Let’s say you have two database records that you want to compare:

import sqlite3
from deepdiff import DeepDiff

conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute("INSERT INTO users (name, age) VALUES ('John', 30)")
c.execute("INSERT INTO users (name, age) VALUES ('Jane', 25)")
conn.commit()

c.execute("SELECT * FROM users WHERE name='John'")
record1 = c.fetchone()
c.execute("SELECT * FROM users WHERE name='Jane'")
record2 = c.fetchone()

diff = DeepDiff(record1, record2)
print(diff)

This will output:

{‘values_changed’: {“root[‘name’]”: {‘new_value’: ‘Jane’, ‘old_value’: ‘John’}, “root[‘age’]”: {‘new_value’: 25, ‘old_value’: 30}}}

As we can see, DeepDiff has identified the differences between the two database records, including changes in values.

Example 3: Detecting Changes in Python Dictionaries

A third common use case for DeepDiff is detecting changes in Python dictionaries. Let’s say you have two dictionaries that you want to compare:

from deepdiff import DeepDiff

dict1 = {'a': 1, 'b': 2, 'c': {'d': 4, 'e': {'f': 6}}}
dict2 = {'a': 1, 'b': 3, 'c': {'d': 5, 'e': {'f': 6, 'g': 7}}}
diff = DeepDiff(dict1, dict2)
print(diff)

Output:

{‘values_changed’: {“root[‘b’]”: {‘new_value’: 3, ‘old_value’: 2}, “root[‘c’][‘d’]”: {‘new_value’: 5, ‘old_value’: 4}, “root[‘c’][‘e’][‘g’]”: {‘new_value’: 7}}}

As we can see, DeepDiff has identified the differences between the two dictionaries, including added and removed dictionary items.

Example 4: Comparing Lists of Complex Objects

Finally, let’s look at an example of comparing lists of complex objects. Let’s say you have two lists of dictionaries representing users and you want to compare them:

from deepdiff import DeepDiff

users1 = [{'id': 1, 'name': 'John', 'age': 30}, {'id': 2, 'name': 'Jane', 'age': 25}]
users2 = [{'id': 1, 'name': 'John', 'age': 30}, {'id': 2, 'name': 'Bob', 'age': 35}]
diff = DeepDiff(users1, users2)
print(diff)

This will output:

{‘values_changed’: {“root[1][‘name’]”: {‘new_value’: ‘Bob’, ‘old_value’: ‘Jane’}, “root[1][‘age’]”: {‘new_value’: 35, ‘old_value’: 25}}}

As we can see, DeepDiff has identified the differences between the two lists of dictionaries, including changes in values.

DeepDiff simplifies debugging

In this tutorial, we have explored some practical examples of using DeepDiff to simplify the debugging process in Python. By leveraging the power of DeepDiff, we can quickly and easily compare and analyze complex data structures, making it much easier to identify and fix issues in our code. Whether you are working with JSON objects, database records, or complex Python data structures, DeepDiff is an invaluable tool for any Python developer.

--

--