1

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

2
  • can you print the stack trace Commented Jun 27, 2018 at 7:08
  • 1
    You should consider using pypi.org/project/jsonmerge Commented Jun 27, 2018 at 7:09

1 Answer 1

1

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

6 Comments

I just checked it and it turns out that merged json file is empty. The files I am loading has proper json format.
@fireandwind I just edited my answer with what I think is the real issue.
it returns again the same empty files but error is different now, fp.write(chunk) TypeError: a bytes-like object is required, not 'str'
@fireandwind Don't open the files in binary mode. JSON is text. Open the files in text mode with 'r' and 'w' modes.
thank you, that worked. This might be different question for stackoverflow. all my json file starts from [{"type": "FeatureCollection" concating is working now but at the end of each json file, I will have [{"type": "FeatureCollection" is there any way which I can keep this only once ?
|

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.