L
L
LordOftheCode2021-11-24 20:25:46
Python
LordOftheCode, 2021-11-24 20:25:46

How to complement json python, do you need code?

I get 3 data id, login, password in the telegram bot.

I need to correctly supplement this json file with this data, which is currently available

{
    "id": {
        "login": "login",
        "password": "password"
    }
}


how can i add it every time?
I tried using the 'a' method, but json breaks every time

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2021-11-24
@LordOftheCode

my json breaks every time

If everything is done correctly, nothing will break:
import json
data = '''{
    "id": {
        "login": "login",
        "password": "password"
    }
}'''

json_data = json.loads(data)
json_data['1'] = {'login': 'log', 'password': 'pass'}

print(json.dumps(json_data, indent=4))
# {
#     "id": {
#         "login": "login",
#         "password": "password"
#     },
#     "1": {
#         "login": "log",
#         "password": "pass"
#     }
# }

D
Denis Melnikov, 2021-11-24
@Mi11er

Well, if it’s straight on the forehead, then parse json into a dict, add a new one and save it again

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question