0

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.

2
  • This might help: stackoverflow.com/a/33054552 Commented Oct 4, 2018 at 20:03
  • cat 1.json test.json two.json > merge_json.json Commented Oct 4, 2018 at 20:21

1 Answer 1

2

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:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.