Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove key but keep value in JSON [Python]

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{

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

>Solution :

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

result = list(map(flatten_data, source)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading