How to merge two json string in Python?

后端 未结 6 2089
醉酒成梦
醉酒成梦 2020-12-08 10:20

I recently started working with Python and I am trying to concatenate one of my JSON String with existing JSON String. I am also working with Zookeeper so I get the existing

6条回答
  •  鱼传尺愫
    2020-12-08 11:03

    To append key-value pairs to a json string, you can use dict.update: dictA.update(dictB).

    For your case, this will look like this:

    dictA = json.loads(jsonStringA)
    dictB = json.loads('{"error_1395952167":"Error Occured on machine h1 in datacenter dc3 on the step2 of process test"}')
    
    dictA.update(dictB)
    jsonStringA = json.dumps(dictA)
    

    Note that key collisions will cause values in dictB overriding dictA.

提交回复
热议问题