简体   繁体   中英

Merge multiple json files in one json file

I have alot of json files like the following:

eg

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.

The b in the mode opens the file in binary mode.

with open("merged_file.json", "wb") as outfile:

But json.dump writes a string, not bytes. That is because it may contain unicode characters and it's outside the scope of json to encode it (eg to utf8 ). You can simply open the output file as text by removing the b .

with open("merged_file.json", "w") as outfile:

It will use he default file encoding. You can also specify the encoding with the open command. eg:

with open("merged_file.json", "w", encoding="utf8") as outfile:

You should also open your file in text mode for the same reasons:

with open(f, "r") as infile:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM