I have alot 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, "rb") as infile:
try:
result.append(json.load(infile))
except ValueError:
print(f)
with open("merged_file.json", "wb") as outfile:
json.dump(result, outfile)
But it is not working, I have the following error:
merged_file.json
Traceback (most recent call last):
File "Data.py", line 13, in <module>
json.dump(result, outfile)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\json\__init__.py", line 180, in dump
fp.write(chunk)
TypeError: a bytes-like object is required, not 'str'
Appreciated for any help.