Created
December 7, 2018 22:49
-
-
Save abdullahceylan/358c568653394847a55c270a62761a6d to your computer and use it in GitHub Desktop.
Merge all .json file in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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