Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge JSON Files
#1
I want to incrementally merge multiple json files of similar structure into a single file that already exists.

My code is:

import glob

read_files = glob.glob("/dbfs/mnt/sourton-house/File*.json")
with open("/dbfs/mnt/sourton-house/mergedJsonFile.json", "w") as outfile:
    outfile.write('{}'.format('\n'.join([open(f, "r").read() for f in read_files])))
The problem is that it over-writes mergedJsonFile.json. I want the files to merge into mergedJsonFile.json
Reply
#2
you open the file in write mode 'w', thus it overwrite the file if it exists. You want to open it in append mode 'a'

that being said, what you do will not produce valid JSON. In extremely unlikely case it may yield ndjson file.
How you will merge multiple json files will depend on their structure and expected result file.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
As buran suggests, probably the right thing to do is read the file, parse it into Python objects (with the json module), do whatever combining you need to and write it out.
Reply
#4
@ndc85430... Thanks a lot for your feedback. I am new to Python so have no idea how to further parse the json file any further before outputing to a json file. Please can you help with the code to do that? Or point me to a resource I can get the information I need

@buran... You are right.'a' in place of 'w' produces an invalid json file. I am completely new to python. Can you help with a code to resolve please?
Reply
#5
I already did - look at the docs for the json module in the standard library. There are functions like loads that will take a string containing JSON and parse it into the relevant Python objects.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  merge all xlsb files into csv mg24 0 345 Nov-13-2023, 08:25 AM
Last Post: mg24
  merge two csv files into output.csv using Subprocess mg24 9 1,799 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Merge all json files in folder after filtering deneme2 10 2,373 Sep-18-2022, 10:32 AM
Last Post: deneme2
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,604 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  How to merge all the files in a directory in to one file sutra 3 2,655 Dec-10-2020, 12:09 AM
Last Post: sutra
  How to read multiple csv files and merge data rajeshE 0 1,955 Mar-28-2020, 04:01 PM
Last Post: rajeshE
  extract specific data from a group of json-files ledgreve 3 3,302 Dec-05-2019, 07:57 PM
Last Post: ndc85430
  error merge text files ledgreve 3 2,708 Nov-18-2019, 12:41 PM
Last Post: DeaD_EyE
  Download multiple large json files at once halcynthis 0 2,789 Feb-14-2019, 08:41 AM
Last Post: halcynthis
  merge files AGC 4 3,918 Oct-04-2017, 08:54 PM
Last Post: AGC

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020