'I need the value of the key in this nested json using python

{
    "users": [
        {
            "2407": {
                "gmail": "[email protected]",
                "name": "Shaurya",
                "address": "19B/11 Kamla Nehru Road Civillines Prayagraj",
                "phn": "9161461745",
                "balance":0
            }
        }
    ]
}

I need to read this json and change the balance key to 100

I tried using :

with open(filename) as f:
        data = json.load(f)
        print(data["users"]["2407"]["balance"])

But its not working



Solution 1:[1]

import json

with open("temp.json") as f:
        data = json.load(f)
        print(data)
        data["users"][0]["2407"]["balance"] = 100
        print(data)

Here. You forgot that users is an array

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aashish Peepra