How to dynamically build a JSON object with Python?

后端 未结 5 1771
没有蜡笔的小新
没有蜡笔的小新 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:37

    There is already a solution provided which allows building a dictionary, (or nested dictionary for more complex data), but if you wish to build an object, then perhaps try 'ObjDict'. This gives much more control over the json to be created, for example retaining order, and allows building as an object which may be a preferred representation of your concept.

    pip install objdict first.

    from objdict import ObjDict
    
    data = ObjDict()
    data.key = 'value'
    json_data = data.dumps()
    

提交回复
热议问题