user96564
user96564

Reputation: 1607

Concating / Merging of each Json Files, python

I have multiple Json files and I want to concat / Merge and make that one Json files. Below code is throwing me an error

def merge_JsonFiles(*filename):
    result = []
    for f1 in filename:
        with open(f1, 'rb') as infile:
            result.append(json.load(infile))

    with open('Mergedjson.json', 'wb') as output_file:
        json.dump(result, output_file)

    # in this next line of code, I want to load that Merged Json files
    #so that I can parse it to proper format
    with open('Mergedjson.json', 'rU') as f:
        d = json.load(f)

Below is the code for my input json file

if __name__ == '__main__':
        allFiles = []
        while(True):
            inputExtraFiles = input('Enter your other file names. To Ignore    this, Press Enter!: ')
            if inputExtraFiles =='':
                break
            else:
                allFiles.append(inputExtraFiles)
        merge_JsonFiles(allFiles)

But it is throwing me an error

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Also, I want to make sure that if it gets only one input file from the console, the merging of json should not throw the error.

Any help, why is this throwing an error ?

Update

it turns that it returning me an empty Mergedjson files. I have valid json format

Upvotes: 1

Views: 4045

Answers (1)

blhsing
blhsing

Reputation: 106445

The error message json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) indicates that your JSON file is malformed or even empty. Please double-check that the files are valid JSON.

Also, there's no good reason for your filename parameter to be a variable-length argument list:

def merge_JsonFiles(*filename):

Remove the * operator so that your JSON files can actually be read according to the filename list.

def merge_JsonFiles(filename):

Upvotes: 1

Related Questions