How to dynamically build a JSON object with Python?

后端 未结 5 1782
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 18:04

I am new to Python and I am playing with JSON data. I would like to dynamically build a JSON object by adding some key-value to an existing JSON object.

I tried the

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 18:26

    You can create the Python dictionary and serialize it to JSON in one line and it's not even ugly.

    my_json_string = json.dumps({'key1': val1, 'key2': val2})
    

提交回复
热议问题