Skip to content

Instantly share code, notes, and snippets.

@abdullahceylan
Created December 7, 2018 22:49
Show Gist options
  • Save abdullahceylan/358c568653394847a55c270a62761a6d to your computer and use it in GitHub Desktop.
Save abdullahceylan/358c568653394847a55c270a62761a6d to your computer and use it in GitHub Desktop.
Merge all .json file in a directory
# You have file1.json and file2.json files.
# Each file has structure:
# [{"key1": "value1"}] - (in file1)
# [{"key2": "value2"}] - (in file2)
# And your goal to merge them and get next view:
# [{"key1": "value1"},
# {"key2": "value2"}]
import json
import glob
result = []
for f in glob.glob("*.json"):
with open(f, "rb") as infile:
result.append(json.load(infile))
with open("merged_file.json", "wb") as outfile:
json.dump(result, outfile, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment