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

Viewed 129

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"
                }
              ]
            }

   ]
}
1 Answers

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}}
Related