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.

1

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.