Jan Tamm
Jan Tamm

Reputation: 117

Merge multiple json files in one json file

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.

Upvotes: 0

Views: 7936

Answers (1)

de1
de1

Reputation: 3124

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 (e.g. 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. e.g.:

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:

Upvotes: 2

Related Questions