Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 700 Bytes

merge-multiple-json-files.md

File metadata and controls

27 lines (22 loc) · 700 Bytes
title description author tags
Merge Multiple JSON Files
Merges multiple JSON files into one and writes the merged data into a new file.
axorax
json,merge,file
import json

def merge_json_files(filepaths, output_filepath):
    merged_data = []

    # Read each JSON file and merge their data
    for filepath in filepaths:
        with open(filepath, 'r') as file:
            data = json.load(file)
            merged_data.extend(data)

    # Write the merged data into a new file
    with open(output_filepath, 'w') as file:
        json.dump(merged_data, file, indent=4)

# Usage:
files_to_merge = ['file1.json', 'file2.json']
merge_json_files(files_to_merge, 'merged.json')