I have a lot of JSON files like the following:
E.g.
1.json
{"name": "one", "description": "testDescription...", "comment": ""}
test.json
{"name": "test", "description": "testDescription...", "comment": ""}
two.json
{"name": "two", "description": "testDescription...", "comment": ""}
...
I want to merge them all in one JSON file like:
merge_json.json
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
I have the following code:
import json
import glob
result = []
for f in glob.glob("*.json"):
with open(f, "r") as infile:
try:
result.append(json.load(infile))
except ValueError as e:
print(f,e)
result = '\n'.join(result)
with open("merged.json", "w", encoding="utf8") as outfile:
json.dump(result, outfile)
I can merge all files, but everything are in a one line how can I add break-line, after adding each file:
instead of:
{"name": "one", "description": "testDescription...", "comment": ""},{"name": "test", "description": "testDescription...", "comment": ""},{"name": "two", "description": "testDescription...", "comment": ""}
Have them like:
merge_json.json
{"name": "one", "description": "testDescription...", "comment": ""},
{"name": "test", "description": "testDescription...", "comment": ""},
{"name": "two", "description": "testDescription...", "comment": ""}
Appreciated for any help.