0

I am having trouble extracting data from nested json in python. I want to create a one column pandas dataframe of all the values of "bill", e.g.

bill
----
a1
a2
a3

Using the output from an API formatted like this:

{
  "status": "succeeded",
  "travels": [
    {
      "jobs": [
        {
          "bill": "a1"
        },
        {
          "bill": "a2"
        },
        {
          "bill": "a3"
        }
      ],
      "vehicle": {
        "plate": "xyz123"
      }
    }
  ]
}

Loading the json directly into pandas gives me only the first instance of 'bill'. I have tried json_normalize() on 'jobs', but it has a key error. Can anybody help me figure out how to grab just the 'bill'?

Thanks

1 Answer 1

1

I think you were on the right track with json_normalize. With your input as a python dictionary d:

from pandas.io.json import json_normalize
json_normalize(d, record_path=['travels', 'jobs'])
  bill
0   a1
1   a2
2   a3
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That did the trick. I wasn't aware you didn't have to specify 'data' in json_normalize
Glad to help! Yeah, json_normalize can be finicky to get working sometimes.

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.