Remove key but keep value in JSON [Python]

Question:

I need to "Semi-Flatten" a JSON object where i have a JSON object with nested items. I have tried to use the flat_json with pandas and other "flatten_json" and json_normalize code in stackoverflow but i end up with completely flatten JSON (something i do not need).

Here is the JSON structure:

[{
    "Stat": {
        "event": "03458188-abf9-431c-8144-ad49c1d069ed",
        "id": "102e1bb1f28ca44b70d02d33380b13",
        "number": "1121",
        "source": "",
        "datetime": "2023-01-13T00:00:00Z",
        "status": "ok"
    },
    "Goal": {
        "name": "goalname"
    },
    "Fordel": {
        "company": "companyname"
    },
    "Land": {
        "name": "landname"
    }
}, {
    "Stat": {
        "event": "22222",
        "id": "44444",
        "number": "5555",
        "source": "",
        "datetime": "2023-01-13T00:00:00Z",
        "status": "ok"
    },
    "Goal": {
        "name": "goalname2"
    },
    "Fordel": {
        "company": "companyname2"
    },
    "Land": {
        "name_land": "landname2"
    }
}]

The result i need is this:

[{

    "event": "03458188-abf9-431c-8144-ad49c1d069ed",
    "id": "102e1bb1f28ca44b70d02d33380b13",
    "number": "1121",
    "source": "",
    "datetime": "2023-01-13T00:00:00Z",
    "status": "ok",
    "name": "goalname",
    "company": "companyname",
    "name_land": "landname"
}, {
    "event": "22222",
    "id": "44444",
    "number": "5555",
    "source": "",
    "datetime": "2023-01-13T00:00:00Z",
    "status": "ok",
    "name": "goalname2",
    "company": "companyname2",
    "name_land": "landname2"
}]

If this can be used with pandas or other json packages it would be great.

Coded i have tried: (copy/paste from another question/answer)

def flatten_data(y):
    out = {}

    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x

    flatten(y)
    return out

That gives me:

{
    "0_event": "03458188-abf9-431c-8144-ad49c1d069ed",
    "0_id": "102e1bb1f28ca44b70d02d33380b13",
      ......
    "1_event": "102e1bb1f28ca44b70d02d33380b13",
    "1_id": "102e1bb1f28ca44b70d02d33380b13",
      
    etc...
}
Asked By: Ali Durrani

||

Answers:

Map the flatten_data function over the list instead of flattening the entire list.

result = list(map(flatten_data, source)))
Answered By: Barmar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.