Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

python - How to merge 2 nested json objects which may have similar values without duplicates?

i have to Api responses that i have to merge (add the missing ColData that has another value and if the same ColData is available in both add up their values. The way that i wanted to do it is just loop through both at the same time (can't figure out how) and start comparing:

1 - if they match go deeper until i find something that doesnt and if its values then just add them up if not just copy append the row data
2 - if they dont match just append the whole col and row

This is example is only about the first condition since i cant figure it out. The 2 json objects look like this:

#not valid json just used for an example
one={
        "Header": {
          "ColData": [
            {
              "value": "col1"
            },
            {
              "value": ""
            },
            {
              "value": ""
            },
            {
              "value": ""
            }
          ]
        },
        "Rows": {
          "Row": [
            {
              "ColData": [
                {
                  "value": "data1",
                  "id": "1"
                },
                {
                  "value": "1"
                },
                {
                  "value": ""
                },
                {
                  "value": "1"
                }
              ],
              "type": "Data"
            },
            {
              "ColData": [
                {
                  "value": "data2",
                  "id": "2"
                },
                {
                  "value": "2"
                },
                {
                  "value": ""
                },
                {
                  "value": "2"
                }
              ]
            }
   ]
}

two={
        "Header": {
          "ColData": [
            {
              "value": "col1"
            },
            {
              "value": ""
            },
            {
              "value": ""
            },
            {
              "value": ""
            }
          ]
        },
        "Rows": {
          "Row": [
            {
              "ColData": [
                {
                  "value": "data1",
                  "id": "12"
                },
                {
                  "value": "1" 
                },
                {
                  "value": ""
                },
                {
                  "value": "1"
                }
              ],
              "type": "Data"
            },
            {
              "ColData": [
                {
                  "value": "data 3",
                  "id": "32"
                },
                {
                  "value": "2"
                },
                {
                  "value": ""
                },
                {
                  "value": "2"
                }
              ]
            }
   ]
}

And i should get something that looks like this:

final={
        "Header": {
          "ColData": [
            {
              "value": "col1"
            },
            {
              "value": ""
            },
            {
              "value": ""
            },
            {
              "value": ""
            }
          ]
        },
        "Rows": {
          "Row": [
            {
              "ColData": [
                {
                  "value": "data1",
                  "id": "1"
                },
                {
                  "value": "2" # the values have been summed since the column has the same value in both
                },
                {
                  "value": ""
                },
                {
                  "value": "2" # the values have been summed since the column has the same value in both
                }
              ],
              "type": "Data"
            },
            {
              "ColData": [
                {
                  "value": "data2",
                  "id": "2"
                },
                {
                  "value": "2"
                },
                {
                  "value": ""
                },
                {
                  "value": "2"
                }
              ]
            },
{
              "ColData": [
                {
                  "value": "data 3",
                  "id": "32"
                },
                {
                  "value": "2"
                },
                {
                  "value": ""
                },
                {
                  "value": "2"
                }
              ]
            }

   ]
}
question from:https://stackoverflow.com/questions/65883455/how-to-merge-2-nested-json-objects-which-may-have-similar-values-without-duplica

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe mergedeep?:

pip3 install mergedeep

And then:

from mergedeep import merge


dict1 = {"one": 1}
dict2 = {"two": {"one_one": 11}}
dict3 = {"two": {"two_two": 22}}

merge(dict1, dict2, dict3) 

print(dict1)
# {"one": 1, "two": {"one_one": 11, "two_two": 22}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...