Home > database >  How to make changes to a JSON file and save them?
How to make changes to a JSON file and save them?

Time:07-24

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 a 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

{
    "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"])

Resulting 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",
                }
            ]
        }

    ]
}

CodePudding user response:

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)
  • Related