How to Merge Multiple JSON Files Using Python
Last Updated :
24 Apr, 2025
We are given multiple JSON files and our task is to merge those multiple JSON files into a single JSON file with the help of different approaches in Python. In this article, we will see how we can merge multiple JSON files in Python.
JSON Files
Below are the two JSON files that we will use in our article to merge them into a single JSON file.
f.json
{"key1":"value1"}
s.json
{"key2": "value2"}
Merging Multiple JSON Files in Python
Below are some of the ways by which we can merge multiple JSON files in Python:
- Using json Module
- Using List Comprehension
- Using os Module with json Module
- Using glob Module with json Module
- Using Pandas Library
Merge Multiple JSON Files Using json Module
In this example, a Python function merge_json_files
is defined to combine data from multiple JSON files specified by file_paths
into a list called merged_data
. The merged data is then written to a new JSON file named "merged.json," and a confirmation message is printed.
Python3
import json
def merge_json_files(file_paths, output_file):
merged_data = []
for path in file_paths:
with open(path, 'r') as file:
data = json.load(file)
merged_data.append(data)
with open(output_file, 'w') as outfile:
json.dump(merged_data, outfile)
file_paths = ["f.json", "s.json"]
output_file = "merged.json"
merge_json_files(file_paths, output_file)
print(f"Merged data written to '{output_file}'")
Output:
merged.json
[{"key1": "value1"}, {"key2": "value2"}]
Merge Multiple JSON Files Using List Comprehension
In this example, the merge_json_files
function reads JSON data from multiple files specified by file_paths
using a list comprehension. The data is then stored in a list named merged_data
. Subsequently, the combined data is written to a new JSON file, "merged.json," using the json.dump()
method. Finally, the merged data list is printed for confirmation.
Python
import json
def merge_json_files(file_paths):
merged_data = [json.load(open(path, 'r')) for path in file_paths]
return merged_data
file_paths = ["f.json", "s.json","e.json","t.json"]
output_file = "merged.json"
merged_data = merge_json_files(file_paths)
with open(output_file, 'w') as outfile:
json.dump(merged_data, outfile)
print(merged_data)
Output:
merged.json
[{"key1": "value1"}, {"key2": "value2"}, {"key2": "value2"}, {"key2": "value2"}]
Merge Multiple JSON Files Using os Module with json Module
In this example, the merge_json_files
function reads and merges JSON files from the specified directory ("./files"
). The combined data is then written to a new JSON file named "merged.json," and the merged data is printed for confirmation.
Python3
import json
import os
def merge_json_files(directory_path):
merged_data = []
for filename in os.listdir(directory_path):
if filename.endswith('.json'):
with open(os.path.join(directory_path, filename), 'r') as file:
data = json.load(file)
merged_data.append(data)
return merged_data
directory_path = "./files"
output_file = "merged.json"
merged_data = merge_json_files(directory_path)
with open(output_file, 'w') as outfile:
json.dump(merged_data, outfile)
print(merged_data)
Output:
merged.json
[{"key1": "value1"}, {"key2": "value2"}]
Merge Multiple JSON Files Using glob Module with json Module
In this example, the merge_json_files
function utilizes the glob
module to obtain a list of JSON file paths from the specified directory ("./files"
). It then reads and merges the JSON data from these files into a list named merged_data
. The combined data is subsequently written to a new JSON file, "merged.json," and the merged data is printed for confirmation.
Python3
import json
import glob
def merge_json_files(directory_path):
merged_data = []
file_paths = glob.glob(directory_path + '/*.json')
for path in file_paths:
with open(path, 'r') as file:
data = json.load(file)
merged_data.append(data)
return merged_data
directory_path = "./files"
output_file = "merged.json"
merged_data = merge_json_files(directory_path)
with open(output_file, 'w') as outfile:
json.dump(merged_data, outfile)
print(merged_data)
Output:
merged.json
[{"key1": "value1"}, {"key2": "value2"}]
Merge Multiple JSON Files Using Pandas Library
In this example, the merge_json_files
function reads JSON data from multiple files specified by file_paths
and merges them into a Pandas DataFrame named merged_data
. The combined data is then written to a new JSON file, "merged.json," using the to_json
method with the 'records' orientation. Finally, the merged DataFrame is printed for confirmation.
Python3
import pandas as pd
import json
def merge_json_files(file_paths):
merged_data = pd.DataFrame()
for path in file_paths:
with open(path, 'r') as file:
data = json.load(file)
row = pd.DataFrame([data])
merged_data = pd.concat([merged_data, row], ignore_index=True)
return merged_data
file_paths = ["f.json", "s.json", "e.json", "t.json"]
output_file = "merged.json"
merged_data = merge_json_files(file_paths)
merged_data.to_json(output_file, orient='records')
print(merged_data)
Output:
merged.json
[{"key1": "value1", "key2": null}, {"key1": null, "key2": "value2"}, {"key1": null, "key2": "value2"}, {"key1": null, "key2": "value2"}]
Similar Reads
Replace Multiple Lines From A File Using Python
In Python, replacing multiple lines in a file consists of updating specific contents within a text file. This can be done using various modules and their associated functions. In this article, we will explore three different approaches along with the practical implementation of each approach in term
3 min read
Exporting Multiple Sheets As Csv Using Python
In data processing and analysis, spreadsheets are a common format for storing and manipulating data. However, when working with large datasets or conducting complex analyses, it's often necessary to export data from multiple sheets into a more versatile format. CSV (Comma-Separated Values) files are
3 min read
Modify Json Fields Using Python
We have a task to modify JSON fields using Python and print the result. In this article, we will see some generally used methods for modifying JSON fields using Python. Example: Input : '{"name": "Alice", "age": 30, "city": "Los Angeles"}' , age : 31Output : '{"name": "Alice", "age": 31, "city": "Lo
3 min read
Extract Multiple JSON Objects from one File using Python
Python is extremely useful for working with JSON( JavaScript Object Notation) data, which is a most used format for storing and exchanging information. However, it can become challenging when dealing with multiple JSON objects stored within a single file. In this article, we will see some techniques
3 min read
Convert Bytes To Json using Python
When dealing with complex byte data in Python, converting it to JSON format is a common task. In this article, we will explore different approaches, each demonstrating how to handle complex byte input and showcasing the resulting JSON output. Convert Bytes To JSON in PythonBelow are some of the ways
2 min read
Write Multiple Variables to a File using Python
Storing multiple variables in a file is a common task in programming, especially when dealing with data persistence or configuration settings. In this article, we will explore three different approaches to efficiently writing multiple variables in a file using Python. Below are the possible approach
2 min read
How to Append Multiple Items to a List in Python
Appending multiple items to a list in Python can be achieved using several methods, depending on whether you want to extend the list with individual elements or nested collections. Letâs explore the various approaches. Using extend method (Adding multiple items from iterable)The list.extend() method
2 min read
Iterate Through Nested Json Object using Python
Working with nested JSON objects in Python can be a common task, especially when dealing with data from APIs or complex configurations. In this article, we'll explore some generally used methods to iterate through nested JSON objects using Python. Iterate Through Nested Json ObjectBelow, are the met
3 min read
How to Parse Nested JSON in Python
We are given a nested JSON object and our task is to parse it in Python. In this article, we will discuss multiple ways to parse nested JSON in Python using built-in modules and libraries like json, recursion techniques and even pandas. What is Nested JSONNested JSON refers to a JSON object that con
3 min read
How To Combine Multiple Lists Into One List Python
Combining multiple lists into a single list is a common operation in Python, and there are various approaches to achieve this task. In this article, we will see how to combine multiple lists into one list in Python. Combine Multiple Lists Into One List PythonBelow are some of the ways by which we ca
2 min read