Pandas json normalize array of objects

Viewed 196

I'm trying to format a json with pandas. but I'm having difficulties.

I tried using the post Pandas json normalize an object property containing an array of objects as an example, but I couldn't.

Json:

{
    "meta": {
        "res_type": "scalar",
        "responses": []
    },
    "data": [
        {
            "type": "scalar_response",
            "attributes": {
                "columns": [
                    {
                        "type": "group",
                        "name": "service",
                        "values": [
                            [
                                "service_aaaaaa"
                            ],
                            [
                                "service_bbbbbb"
                            ]
                        ]
                    },
                    {
                        "type": "number",
                        "meta": {
                            "unit": null
                        },
                        "values": [
                            0.04434908435530647,
                            0.03417774374093277
                        ],
                        "name": "query1"
                    },
                    {
                        "type": "number",
                        "meta": {
                            "unit": [
                                {
                                    "scale_factor": 1.0,
                                    "name": "second",
                                    "family": "time",
                                    "short_name": "s",
                                    "plural": "seconds",
                                    "id": 11
                                },
                                null
                            ]
                        },
                        "values": [
                            44.23442286901232,
                            114.35570798348397
                        ],
                        "name": "query2"
                    },
                    {
                        "type": "number",
                        "meta": {
                            "unit": [
                                {
                                    "scale_factor": 1.0,
                                    "name": "hit",
                                    "family": "cache",
                                    "short_name": null,
                                    "plural": "hits",
                                    "id": 39
                                },
                                null
                            ]
                        },
                        "values": [
                            5.501870351870352,
                            32.807906894100927
                        ],
                        "name": "query3"
                    },
                    {
                        "type": "number",
                        "meta": {
                            "unit": [
                                {
                                    "scale_factor": 1.0,
                                    "name": "error",
                                    "family": "general",
                                    "short_name": "err",
                                    "plural": "errors",
                                    "id": 44
                                },
                                null
                            ]
                        },
                        "values": [
                            null,
                            3.8181818181818185
                        ],
                        "name": "query4"
                    },
                    {
                        "type": "number",
                        "meta": {
                            "unit": null
                        },
                        "values": [
                            0.0,
                            0.11637992726894603
                        ],
                        "name": "query4 / query3"
                    }
                ]
            }
        }
    ]
}

Expected Output:

service         query1              query2              query3              query4              query4 / query3
service_aaaaaa  0.04434908435530647 44.23442286901232   5.501870351870352   null                0.0
service_bbbbbb  0.03417774374093277 114.35570798348397  32.807906894100927  3.8181818181818185  0.11637992726894603

I tried in a few ways but was unsuccessful.

1 Answers
import json

import pandas as pd

d = json.loads(text)

pd.DataFrame(dict((_['name'], _['values']) for _ in d['data'][0]['attributes']['columns']))

enter image description here

text is your json text

Related