4

As part of a Python program, I want to merge JSON objects that contain identically structured data. For instance:

{
    "responseStatus": "SUCCESS",
    "responseDetails": {
        "total": 5754,
    },
    "data": [
        {
            "id": 1324651
        },
        {
            "id": 5686131
        }
    ]
}

What I want to do is to add the content of the data array of my section object into the data array of my first object.

So, assuming:

thejson1 = json.loads({"responseStatus": "SUCCESS","responseDetails": {"total": 5754,},"data": [{"id": 1324651},{"id": 5686131}]})
thejson2 = json.loads({"responseStatus": "SUCCESS","responseDetails": {"total": 1234,},"data": [{"id": 2165735},{"id": 2133256}]})

I thought that executing:

thejson1["data"].append(thejson2["data"])

Would expand thejson1 into:

{
    "responseStatus": "SUCCESS",
    "responseDetails": {
        "total": 5754,
    },
    "data": [
        {
            "id": 1324651
        },
        {
            "id": 5686131
        },
        {
            "id": 2165735
        },
        {
            "id": 2133256
        }
    ]
}

But what it does instead is add thejson2 data as an array within the data array of thejson1:

{
    "responseStatus": "SUCCESS",
    "responseDetails": {
        "total": 5754,
    },
    "data": [
        {
            "id": 1324651
        },
        {
            "id": 5686131
        },
        [
            {
                "id": 2165735
            },
            {
                "id": 2133256
            }
        ]
    ]
}

So, what am I doing wrong? It looks like append adds the data array of the second JSON object instead of its content, but note that I can't know in advance the contents of the "data" array in my JSON input, so I can't write code that specifically loops in the "id" objects to add them one by one.

Thanks in advance!

R.

mrgou
  • 1,576
  • 2
  • 21
  • 45
  • Possible duplicate of [Difference between append vs. extend list methods in Python](https://stackoverflow.com/questions/252703/difference-between-append-vs-extend-list-methods-in-python) – Grigoriy Mikhalkin Apr 13 '18 at 08:13

2 Answers2

9

You're looking for extend, not append.

thejson1["data"].extend(thejson2["data"])

append takes the single argument and insert it to the end. While extend extends the list by adding all the individual values in the argument list to the end.

# example:
a=[1, 2, 3]

b = a[:].append([4, 5])
# b = [1, 2, 3, [4, 5]]

c = a[:].extend([4, 5])
# c = [1, 2, 3, 4, 5]
Taku
  • 31,927
  • 11
  • 74
  • 85
1
thejson1 = {"responseStatus": "SUCCESS","responseDetails": {"total": 5754,},"data": [{"id": 1324651},{"id": 5686131}]}
thejson2 = {"responseStatus": "SUCCESS","responseDetails": {"total": 1234,},"data": [{"id": 2165735},{"id": 2133256}]}

thejson1["data"]  += thejson2["data"]

Output:

{'responseDetails': {'total': 5754}, 'data': [{'id': 1324651}, {'id': 5686131}, {'id': 2165735}, {'id': 2133256}], 'responseStatus': 'SUCCESS'}

You can also use += to extend.

Rakesh
  • 81,458
  • 17
  • 76
  • 113