skip to Main Content
{
  "JSON_DATA": [
    {
      "id": 0,
      "name": "Name",
      "image": "food_0.jpg",
      "kcal": "32",
      "url": "bat",
      "desc": "null",
      "time": "null",
      "first_unit": "Unit1",
      "carb": "72",
      "protein": "23",
      "fat": "4",
      "units": [
        {
          "unit": "Unit1",
          "amount": "15.0000",
          "calory": "32.4877372383",
          "carbohydrt": "5.44751985283",
          "protein": "1.75822099387",
          "fat": "0.155740956864",
          "calcium": "8.71466176412",
          "cholestrl": "0.0",
          "fiber_td": "1.85840511552",
          "iron": "0.711263854542",
          "lipid_tot": "0.155740956864",
          "potassium": "87.793890567",
          "sodium": "15.666368442",
          "vit_a_iu": "11.8943218304",
          "vit_c": "2.25616660116"
        },
        {
          "unit": "Unit2",
          "amount": "110.0000",
          "calory": "238.243406414",
          "carbohydrt": "39.9484789208",
          "protein": "12.8936206218",
          "fat": "1.14210035034",
          "calcium": "63.9075196035",
          "cholestrl": "0.0",
          "fiber_td": "13.6283041805",
          "iron": "5.21593493331",
          "lipid_tot": "1.14210035034",
          "potassium": "643.821864158",
          "sodium": "114.886701908",
          "vit_a_iu": "87.2250267561",
          "vit_c": "16.5452217418"
        },
        {
          "unit": "Unit3",
          "amount": "1.0000",
          "calory": "2.16584914922",
          "carbohydrt": "0.363167990189",
          "protein": "0.117214732925",
          "fat": "0.0103827304576",
          "calcium": "0.580977450941",
          "cholestrl": "0.0",
          "fiber_td": "0.123893674368",
          "iron": "0.0474175903028",
          "lipid_tot": "0.0103827304576",
          "potassium": "5.8529260378",
          "sodium": "1.0444245628",
          "vit_a_iu": "0.792954788692",
          "vit_c": "0.150411106744"
        }
      ]
    }
  ]
}

Hello, I have such a json file. there are very long comma numbers between this json data. I want to edit them, for example, I want to save the value 32.4877372383 of the calory key in the Unit1 object as 32.48.

How can I do this in Python or what other way can I do it? I just want 2 digits left after the dot.

2

Answers


  1. You can use this method

    print("{:.2f}".format(32.4877372383))
    

    You can change 2 in 2f to any number of decimal points you want to show.

    Login or Signup to reply.
  2. import json
    
    with open("data.json", "r") as jsonFile:
        data = json.load(jsonFile)
    
    dicts_list = data["JSON_DATA"][0]["units"]
    for i, d in enumerate(dicts_list):
        for k, v in dicts_list[i].items():
            if '.' in v:
                dicts_list[i][k] = '.'.join(((v.split(".")[0], v.split(".")[1][:2])))
            
    with open("modified_data.json", "w") as jsonFile:
        json.dump(data, jsonFile, indent=4)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search