2

I'm trying to put a JSON result into a DataFrame using python3.

Here is the Json result I'm working with:

{
    "jsonrpc": "2.0",
    "result": {
        "2": {
            "status": "0",
            "problems": [],
            "sla": [
                {
                    "from": 1582066800,
                    "to": 1582116906,
                    "sla": 100,
                    "okTime": 50106,
                    "problemTime": 0,
                    "downtimeTime": 0
                }
            ]
        },
        "3": {
            "status": "0",
            "problems": [],
            "sla": [
                {
                    "from": 1582066800,
                    "to": 1582116906,
                    "sla": 100,
                    "okTime": 50106,
                    "problemTime": 0,
                    "downtimeTime": 0
                }
            ]
        }
   },
    "id": 1
}

So far, I 've been able to get the table for one entry: df = pd.DataFrame(mydata['result']['2']['sla'])

Now I would like to have all entries in my table. Here is what i would like to get:

 hostid   from           to            sla     okTime    problemTime     downtimeTime
 2        1582066800     1582116906    100     50106     0               0
 3        1582066800     1582116906    100     50106     0               0

How can I proceed ?

1

1 Answer 1

1

Try this to grab elements from the different levels you have above:

data = [{'hostid': key, **val['sla'][0]} for key, val in mydata['result'].items()]

df = pd.DataFrame(data)

Doing this before pandas will help if you dataset is large. Otherwise you can use pandas to flatten everything as suggested in the comments.

Sign up to request clarification or add additional context in comments.

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.