Merge multiple JSON objects into one single object in Python

In this article, we will learn how to combine multiple JSON objects into one using python.

The term JSON stands for JavaScript Object Notation. It is a kind of executable file that is used to store and transfer data between web applications.

Once we have the JSON file, we can load or parse it to work with the data in any programming language.

In Python, if we try to load or parse and JSON file with multiple JSON objects, it might throw us some error. It is because json.load() method can read a file with only one valid JSON object.

Therefore, we need to merge multiple JSON objects into a single one before using it with json.load() method in python.

Merge Multiple JSON objects into one single object

To combine multiple JSON objects into one, we have to first convert each object to a dictionary first using json.loads() method and then append it to a single JSON property using append() method.

Let’s understand it with an example.

Suppose, we have a file with multiple employee details as JSON objects.

{"id": 1, "name": "John", "email": "[email protected]"}
{"id": 2, "name": "Alex", "email": "[email protected]"}
{"id": 3, "name": "Tom", "email": "[email protected]"}
{"id": 4, "name": "Jerry", "email": "[email protected]"}

Now, we want to merge all the JSON objects into a single JSON property as a python list.

{"employee_records":[
    // all json objects here
]}

So to merge two or more JSON objects into a single object, we can write the following code as shown below.

Example:

import json

employee_list = {'employee_records':[]}

with open('json.txt') as f:
    for json_obj in f:
        employee_dict = json.loads(json_obj)
        employee_list['employee_records'].append(employee_dict)

print(json.dumps(employee_list))

Output:

{"employee_records": [
    {"id": 1, "name": "John", "email": "[email protected]"}, 
    {"id": 2, "name": "Alex", "email": "[email protected]"}, 
    {"id": 3, "name": "Tom", "email": "[email protected]"}, 
    {"id": 4, "name": "Jerry", "email": "[email protected]"}
  ]
}

As you can see, we have all the JSON objects from the file into a single object in python.

Code Explanation:

First, to use JSON in python, we have to import the built-in package called json.

Next, we read through each valid JSON object from the file using open('json.txt') function.

We used the for loop to iterate through each object and then convert it to a python dictionary using json.loads() method.

employee_dict = json.loads(json_obj)

Once, we have converted the JSON objects to a dictionary, we then append it to the JSON property with an empty list using append() method.

employee_list['employee_records'].append(employee_dict)

After that, we used the json.dumps() method to convert the python object (employee_list) into a json string and print it out in the terminal.

json.dumps(employee_list)

Save the merged JSON object in a file

You can also merge the JSON object and save it in a file by using the following lines.

# json.dumps() converts a python object into json string
valid_json = json.dumps(employee_list)        
f = open("valid_json_file.json", "a")
f.write(valid_json)
f.close()

Here, we have created a file using open() method.

Next, we wrote the content i.e the employee_list to the file using f.write() method. And then closed the file using f.close()

So now, when we run the python script, it will merge the JSON objects and then save them in a single file in our current directory.

Complete Python Program:

import json

employee_list = {'employee_records':[]}

with open('json.txt') as f:
    for json_obj in f:
        employee_dict = json.loads(json_obj) 
        employee_list['employee_records'].append(employee_dict)

valid_json = json.dumps(employee_list)     
f = open("valid_json_file.json", "a")
f.write(valid_json)
f.close()

Conclusion: Here we have learned how to compile more the one JSON objects from a file into one single JSON object and then save the object in a new file in the current directory using python.


Scroll to Top