Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge JSON data with Python

Tags:

python

json

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.

like image 648
mrgou Avatar asked Oct 15 '25 20:10

mrgou


2 Answers

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]
like image 159
Taku Avatar answered Oct 18 '25 13:10

Taku


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.

like image 20
Rakesh Avatar answered Oct 18 '25 14:10

Rakesh