D
D
D55RUS2020-05-11 09:43:12
Python
D55RUS, 2020-05-11 09:43:12

How to add object to json list?

I have this json:

{
    "items": [
        {
            "start_price": "5000",
            "end_price": "4750",
            "classid": "310781333",
            "instanceid": "302028390",
            "name": "P90 | Teardown (Field-Tested)"
        }
    ]
}

How can I make it so that new data is added to "items" and the past is not erased? For example:
{
    "items": [
        {
            "start_price": "5000",
            "end_price": "4750",
            "classid": "310781333",
            "instanceid": "302028390",
            "name": "P90 | Teardown (Field-Tested)"
        }
       {
            "start_price": "3000",
            "end_price": "2750",
            "classid": "310781533",
            "instanceid": "302015782",
            "name": "Souvenir MP5-SD | Lab Rats (Field-Tested)"
        }
    ]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-05-11
@D55RUS

d = json.loads(json_string)
d["items"].append({
    "start_price": "3000",
    "end_price": "2750",
    "classid": "310781533",
    "instanceid": "302015782",
    "name": "Souvenir MP5-SD | Lab Rats (Field-Tested)"
})
json_string = json.dumps(d)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question