user16996079
user16996079

Reputation:

json_normalize, getting 'byte indices must be integers or slices' error from api request of nested json

I retrieved json through an api and ended up with the following:

{"result":
{"1435368134":
{"suggestions":[{"keyword":"mutual insurance company","ranking":3,"is_typo":false,"volume":22,"score":71.33},{"keyword":"home maintenance","ranking":7,"is_typo":false,"volume":19,"score":47.85} ...

I'd like to pull the (keyword) records from suggestions as a dataframe.

This answer seemed to be what I was after Convert JSON API response to pandas Dataframe

However, when I ran the following code:

url = "https://public-api.apptweak.com/api/public/store/keywords/suggestions/app.json?apps=1435368134&country=us&language=us&device=iphone&limit=500&offset=0"

headers = {
    "Accept": "application/json",
    "x-apptweak-key": "xxx"
}

response = requests.get(url, headers=headers)
data = response.json()
df = pd.json_normalize(response, record_path = 'suggestions',meta=['keyword','ranking','is_typo','volume','score'])

I get a 'byte indices must be integers or slices, not str' error.

Have also tried passing response.json() as data, but get the error 'Key error suggestions not found.'

response = requests.get(url, headers=headers)
data = response.json()
df = pd.json_normalize(data, record_path = 'suggestions',meta=['keyword','ranking','is_typo','volume','score'])

So maybe, I just don't understand specifying a key in json.

Upvotes: 0

Views: 259

Answers (1)

user16996079
user16996079

Reputation:

Well, as it turns out, I was correct. I don't know how to specify record_path for json.

Here's the line that did the trick:

df = pd.json_normalize(data,record_path = ['result','1435368134','suggestions'])

The line above (without meta=[]) created a dataframe that listed each item in the suggestions list with the expected columns.

Upvotes: 1

Related Questions