0

I am new to the JSON python lib.

I have this JSON file:

   {
        "usrdata": [
            {
                "user": "---/ LandiPlayz \\---#****",
                "money": 10
            },
            {
                "user": "Snubz#****",
                "money": 10
            }
        ]
    }

and I need to modify the "money" field of one of the users. There will be more added users, so I need to find which one it is by finding the users name. Is that possible, or should I do format the file differently.

Thanks in advance

CodingWithAdin
  • 307
  • 1
  • 7
  • 18
  • You can, you just need to know the user value. No matter how the JSON values are , if the you have the info to reach the value, you'll be good – azro May 28 '20 at 19:17
  • You don't really edit the file; you decode it, modify the resulting in-memory data structure, and write out a new file. – chepner May 28 '20 at 19:20
  • Does this answer your question? [How to update json file with python](https://stackoverflow.com/questions/13949637/how-to-update-json-file-with-python) https://stackoverflow.com/questions/21035762/python-read-json-file-and-modify https://stackoverflow.com/questions/48634389/update-json-file-in-python – user120242 May 28 '20 at 19:30
  • @azro Thanks for that. What do you mean reach? – CodingWithAdin May 28 '20 at 19:38

1 Answers1

2

You have to iterate over the user_infos, and when getting the good one, update the money

json_value =    {
        "usrdata": [
            {"user": "---/ LandiPlayz \\---#****","money": 10},
            {"user": "Snubz#****","money": 10}
        ]
    }
username = "Snubz#****"
new_money = 50

for user_info in json_value["usrdata"]:
    if user_info['user'] == username:
        user_info['money'] = new_money
azro
  • 53,056
  • 7
  • 34
  • 70