Joe
Joe

Reputation: 909

Merge multiple JSON files outputs in one list object

I have a folder that contains many JSON files. I want to write Python code that scans all files, reads them, and merges them into a single list object.

I tried the following.

mypath = 'C:/Users/data'
files = ['f1.josn', 'f2.josn', ..., 'f20.josn']
output_list = []
for file in files:
    with open(mypath+'/'+file, encoding='utf-8') as infile:
        output_list.append(json.load(infile))

I got a traceback after running the above code:

JSONDecodeError: Invalid control character at: line 1002 column 99 (char 38611)

Upvotes: 0

Views: 37

Answers (1)

Leo
Leo

Reputation: 54

The problem is in one (or all) of the json-files. Quoting this page:

If the data being deserialized is not a valid JSON document, a JSONDecodeError will be raised.

Upvotes: 1

Related Questions