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

user96564
  • 1,578
  • 5
  • 24
  • 42

1 Answers1

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):
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • I just checked it and it turns out that merged json file is empty. The files I am loading has proper json format. – user96564 Jun 27 '18 at 07:16
  • @fireandwind I just edited my answer with what I think is the real issue. – blhsing Jun 27 '18 at 07:28
  • it returns again the same empty files but error is different now, `fp.write(chunk) TypeError: a bytes-like object is required, not 'str'` – user96564 Jun 27 '18 at 07:33
  • @fireandwind Don't open the files in binary mode. JSON is text. Open the files in text mode with `'r'` and `'w'` modes. – blhsing Jun 27 '18 at 07:35
  • 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 ? – user96564 Jun 27 '18 at 07:43
  • @fireandwind You're welcome. Yeah there is, but it's better that you post the actual JSON data properly formatted in a new question so that we can tell what to look for. – blhsing Jun 27 '18 at 07:47