Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Separate large JSON object into many different files

I have a JSON file with 10000 data entries like below in a file.

{
    "1":{
        "name":"0",
        "description":"",
        "image":""
    },
    "2":{
        "name":"1",
        "description":"",
        "image":""
     },
     ...
}

I need to write each entry in this object into its own file.

For example, the output of each file looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1.json

{
  "name": "",
  "description": "",
  "image": ""
}

I have the following code, but I’m not sure how to proceed from here. Can anyone help with this?

import json
  
with open('sample.json', 'r') as openfile:
  
    # Reading from json file
    json_object = json.load(openfile)

>Solution :

You can use a for loop to iterate over all the fields in the outer object, and then create a new file for each inner object:

import json

with open('sample.json', 'r') as input_file:
    json_object = json.load(input_file)
    for key, value in json_object.items():
        with open(f'{key}.json', 'w') as output_file:
            json.dump(value, output_file)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading