VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-merge-multiple-json-files-using-python/

⇱ How to Merge Multiple JSON Files Using Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Merge Multiple JSON Files Using Python

Last Updated : 23 Jul, 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:

  1. Using json Module
  2. Using List Comprehension
  3. Using os Module with json Module
  4. Using glob Module with json Module
  5. 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.

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.

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.

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.

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.

Output:

merged.json

[{"key1": "value1", "key2": null}, {"key1": null, "key2": "value2"}, {"key1": null, "key2": "value2"}, {"key1": null, "key2": "value2"}]
Comment