How to Merge Json Files

Merge multiple JSON files (more than two)

You're using the json module to convert the JSON file into Python objects, but you're not using the module to convert those Python objects back into JSON. Instead of this at the end

textfile_merged.write(str(all_items))

try this:

json.dump({ "items": all_items }, textfile_merged)

(Note that this is also wrapping the all_items array in a dictionary so that you get the output you expect, otherwise the output will be a JSON array, not an object with an "items" key).

How to merge multiple json files into one file in python

You should use extend instead of append. It will add the items of the passed list to result instead of a new list:

files=['my.json','files.json',...,'name.json']

def merge_JsonFiles(filename):
result = list()
for f1 in filename:
with open(f1, 'r') as infile:
result.extend(json.load(infile))

with open('counseling3.json', 'w') as output_file:
json.dump(result, output_file)

merge_JsonFiles(files)

how to merge multiple json files into 1 using python

The file format is incorrect.
It should either be a list of dictionary or dictionary containing unique keys

If you cannot modify the file, you can read the content and append it to the result.

Read each file and append the result

result = ''
for f in glob.glob("*.json"):
with open(f, "r") as infile:
result += infile.read()

Then write the final result into another file

with open("merged_file.json", "w") as outfile:
outfile.writelines(result)

Output:

{"playlist_track.PlaylistId":1,"playlist_track.TrackId":3402}
{"playlist_track.PlaylistId":1,"playlist_track.TrackId":3389}
{"playlist_track.PlaylistId":1,"playlist_track.TrackId":3402}
{"playlist_track.PlaylistId":1,"playlist_track.TrackId":3389}

With the above solution I would definitely attempt to change the file extension to .txt or something else that is not JSON.

My recommandation would be to convert the file into a JSON format and save it this way.

Read each line and convert it in to a dict. Result will contain a list of dict, which is JSON serializable

result = []
for f in glob.glob("*.json"):
with open(f, "r") as infile:
for line in infile.readlines():
result.append(json.loads(line))

Once this is done you can now save the content as a JSON file

with open("merged_file.json", "w") as outfile:
json.dump(result, outfile)

You may open the file as a JSON file now:

with open("merged_file.json", "r") as fp:
print(pformat(json.load(fp)))

Output:

[{"playlist_track.PlaylistId": 1, "playlist_track.TrackId": 3402}, {"playlist_track.PlaylistId": 1, "playlist_track.TrackId": 3389}, {"playlist_track.PlaylistId": 1, "playlist_track.TrackId": 3402}, {"playlist_track.PlaylistId": 1, "playlist_track.TrackId": 3389}]

Issue with merging multiple JSON files in Python

You can't just concatenate two JSON strings to make valid JSON (or combine them by tacking ',\n' to the end of each).
Instead, you could combine the two (as Python objects) into a Python list, then use json.dump to write it to a file as JSON:

import json
import glob

result = []
for f in glob.glob("*.json"):
with open(f, "rb") as infile:
result.append(json.load(infile))

with open("merged_file.json", "wb") as outfile:
json.dump(result, outfile)

If you wanted to do it without the (unnecesssary) intermediate step of parsing each JSON file, you could merge them into a list like this:

import glob

read_files = glob.glob("*.json")
with open("merged_file.json", "wb") as outfile:
outfile.write('[{}]'.format(
','.join([open(f, "rb").read() for f in read_files])))


Related Topics



Leave a reply



Submit