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

How can I make changes in the JSON file and save it later in Python or JS?

I have one JSON file and I want to assign the key (food_image) value in it to index value in a loop.. for example food_image0, food_image1 .. food_image{loop index}

Then i want to save this JSON file as JSON file. .All values ​​with food_image key will be prefixed with food_image and will be rewritten by adding the index suffix in the loop. It’s all I want

JSON

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

{
    "DATABASE":
    [
        {
            "food_id": 0,
            "food_name": "Food Name ABC", 
            "food_image": "imagesite/abc.jpg", // food_image_0
            "food_kcal": "32",
            "units":
            [
                {
                    "amount": "15.0000",
                    "calory": "32.4877372383",
                },
                {
                    "amount": "110.0000",
                    "calory": "238.243406414",
                }
            ]
        },
        {
            "food_id": 1,
            "food_name": "Food Name XYZ", 
            "food_image": "imagesite/xyz.jpg", // food_image_1
            "food_kcal": "32",
            "units":
            [
                {
                    "amount": "15.0000",
                    "calory": "32.4877372383",
                },
                {
                    "amount": "110.0000",
                    "calory": "238.243406414",
                }
            ]
        }
        
    ]
}

Python

import json
with open('json_file.json',encoding="utf8") as myfile:
    data=myfile.read()

obj = json.loads(data)
for idx, i in enumerate(obj['DATABASE']):
    print(i["food_image"])


RESULT: JSON

{
    "DATABASE":
    [
        {
            "food_id": 0,
            "food_name": "Food Name ABC", 
            "food_image": "food_image_0.jpg", 
            "food_kcal": "32",
            "units":
            [
                {
                    "amount": "15.0000",
                    "calory": "32.4877372383",
                },
                {
                    "amount": "110.0000",
                    "calory": "238.243406414",
                }
            ]
        },
        {
            "food_id": 1,
            "food_name": "Food Name XYZ", 
            "food_image": "food_image_1.jpg"
            "food_kcal": "32",
            "units":
            [
                {
                    "amount": "15.0000",
                    "calory": "32.4877372383",
                },
                {
                    "amount": "110.0000",
                    "calory": "238.243406414",
                }
            ]
        }
        
    ]
}

>Solution :

Once you read your JSON file into a Python object, mutate the object, then write it back to a file.

In the example below, I create a separate file instead of overwriting the original (which I would recommend you do too in case of bugs should you do other mutations).

import json
import os

source = 'json_file.json'
dest = 'updated.json'

with open(source) as f:
    content = json.load(f)

for i, entry in enumerate(content['DATABASE']):
    _, extension = os.path.splitext(entry['food_image'])
    entry['food_image'] = f'food_image_{i}{extension}'

with open(dest, 'w+') as f:
    json.dump(content, f)
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