Davide Lorino
Davide Lorino

Reputation: 915

Python requests response - cant unnest the data object

I am querying an API and receiving back a response of type text


url = "https://tripadvisor1.p.rapidapi.com/reviews/list"

querystring = {"limit":"20","currency":"USD","lang":"en_US","location_id":"2269364"}

headers = {
    'x-rapidapi-host': "xxx",
    'x-rapidapi-key': "xxx"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

enter image description here

I then want to retrieve a dataframe object. Various online sources suggest this approach:

d = json.loads(response.text)
e = json_normalize(d)

However, what I get back is this:

enter image description here

I would like to obtain a dataframe object from the 'data' column. Please advise if I can make this question clearer.

Upvotes: 0

Views: 130

Answers (1)

tzaman
tzaman

Reputation: 47840

First off requests already has a json() method on the response object to decode JSON responses. Second, the top level object contains three things: data, paging_results, and paging_total_results which is why you see what you do.

If you want to extract the inner data contents, just pull that out before passing it to normalize:

response = ...
df = json_normalize(response.json()['data'])

Upvotes: 2

Related Questions