How to Merge JSON Objects in Python
This guide covers various methods for merging JSON objects (represented as Python dictionaries) in Python. We'll explore using dictionary unpacking, the merge operator (|
and |=
), and dictionary comprehensions. We'll also cover writing the merged JSON to a file.
Merging JSON Objects using Dictionary Unpacking (**
)
Dictionary unpacking provides a concise and readable way to merge dictionaries, particularly when you want to create a new merged dictionary without modifying the originals. This is generally the best approach for simple merges.
import json
obj1 = json.dumps({'id': 1, 'name': 'tom nolan'}) # JSON strings
obj2 = json.dumps({'site': 'tutorialreference.com', 'topic': 'Python'})
dict1 = json.loads(obj1) # Convert to Python dictionaries
dict2 = json.loads(obj2)
merged_dict = {**dict1, **dict2} # Merge using unpacking
print(merged_dict)
# Output: {'id': 1, 'name': 'tom nolan', 'site': 'tutorialreference.com', 'topic': 'Python'}
json.loads()
converts the JSON strings (obj1
,obj2
) to Python dictionaries.{**dict1, **dict2}
: This creates a new dictionary. The**
operator unpacks the key-value pairs from each dictionary. If there are duplicate keys, the values from the later dictionary (dict2
in this case) take precedence.- You can add more keys by writing them after the last unpacked dictionary, like:
import json
obj1 = json.dumps({'id': 1, 'name': 'tom nolan'})
obj2 = json.dumps({'site': 'tutorialreference.com', 'topic': 'Python'})
dict1 = json.loads(obj1)
dict2 = json.loads(obj2)
merged_dict = {**dict1, **dict2, 'salary': 1500, 'job': 'programmer'}
print(merged_dict)
# Output: {'id': 1, 'name': 'tom nolan', 'site': 'tutorialreference.com',
# 'topic': 'Python', 'salary': 1500, 'job': 'programmer'}
Merging JSON Objects using Merge Operators (|
and |=
)
Python 3.9+ introduced dictionary merge operators, providing another concise way to merge dictionaries:
|
(Merge): Creates a new merged dictionary.|=
(Update): Modifies a dictionary in-place.
import json
obj1 = json.dumps({'id': 1, 'name': 'tom nolan'})
obj2 = json.dumps({'site': 'tutorialreference.com', 'topic': 'Python'})
dict1 = json.loads(obj1)
dict2 = json.loads(obj2)
merged_dict = dict1 | dict2 # Merge using | (creates a NEW dictionary)
print(merged_dict)
# Output: {'id': 1, 'name': 'tom nolan', 'site': 'tutorialreference.com', 'topic': 'Python'}
# In-place update using |=
dict1 |= dict2
print(dict1)
# Output: {'id': 1, 'name': 'tom nolan', 'site': 'tutorialreference.com', 'topic': 'Python'}
- If your python version is 3.9 or later, this is a perfectly acceptable way of merging dictionaries.
Merging with Dictionary Comprehensions (Less Recommended)
While possible, dictionary comprehensions are generally less readable and less efficient for simple merges than the previous methods. They are more useful for conditional merging or transformation during the merge.
import json
obj1 = json.dumps({'id': 1, 'name': 'tom nolan'})
obj2 = json.dumps({'site': 'tutorialreference.com', 'topic': 'Python'})
dict1 = json.loads(obj1)
dict2 = json.loads(obj2)
merged_dict = {
key: value for (key, value)
in list(dict1.items()) + list(dict2.items())
}
print(merged_dict)
# Output: {'id': 1, 'name': 'tom nolan', 'site': 'tutorialreference.com', 'topic': 'Python'}
- You first get the items of the dictionaries, concatenate them into a single list, and then use that to construct a new dictionary using dict comprehension.
- This approach is very verbose, and not the easiest to understand.
- You can exclude keys using the
if
statement.
Writing a Merged Dictionary to a JSON File
To save the merged dictionary to a file in JSON format:
import json
obj1 = json.dumps({'id': 1, 'name': 'tom nolan'})
obj2 = json.dumps({'site': 'tutorialreference.com', 'topic': 'Python'})
dict1 = json.loads(obj1)
dict2 = json.loads(obj2)
merged_dict = {**dict1, **dict2}
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(merged_dict, f, indent=4, ensure_ascii=False)
- The
with open('data.json', 'w', encoding='utf-8') as f:
syntax creates a file handle and opens thedata.json
file. json.dump()
serializes themerged_dict
object into a JSON formatted stream.indent=4
pretty-prints the JSON with an indentation of 4 spaces (optional, but improves readability).ensure_ascii=False
allows non-ASCII characters to be written to the file directly (important for UTF-8 encoding).- You can sort keys using the
sort_keys
parameter.json.dump(merged_dict, f, indent=4, ensure_ascii=False, sort_keys=True)