Reputation: 81
I am trying since hours and a lot of search to get a dataframe from my Pocket API list retrieve.
But my code adds everything up in only one row finally, what is not what I want for sure.
I tried a lot but don't find the error.
My code looks like this:
import json
import pandas as pd
from pandas.io.json import json_normalize
from flatten_json import flatten
import collections
import requests
from urllib.parse import urlencode
from urllib.request import Request, urlopen
# GET request for data in JSON format
parameters = {
'consumer_key' : '9...9',
'access_token' : 'c...b',
'detailType' : 'complete',
'count' : '10'
}
response = requests.get('https://getpocket.com/v3/get', params=parameters)
json_string = response.json()
df=pd.json_normalize(json_string)
print(df)
The result looks like this:
status complete error ... list.3483242700.domain_metadata.greyscale_logo list.3483242700.listen_duration_estimate search_meta.search_type
0 1 1 None ... https://logo.clearbit.com/washingtonpost.com?s... 0 normal
The source JSON resonse.text looks like this:
{"status":1,"complete":1,"list":{"237938806":{"item_id":"237938806","resolved_id":"237938806","given_url":"https:\/\/getpocket.com\/developer\/docs\/v3\/retrieve","given_title":"Pocket Developer Program: Pocket API: Retrieve","favorite":"0"
....
washpost.s3.amazonaws.com\/public\/SMWO67R4BAI6XKWZRFMSE4UAYQ.jpg&w=1440","domain_metadata":{"name":"The Washington Post","logo":"https:\/\/logo.clearbit.com\/washingtonpost.com?size=800","greyscale_logo":"https:\/\/logo.clearbit.com\/washingtonpost.com?size=800&greyscale=true"},"listen_duration_estimate":0}},"error":null,"search_meta":{"search_type":"normal"},"since":1638029019}
Upvotes: 0
Views: 109
Reputation: 2723
Try df = pd.json_normalize(json_string["list"].values())
.
From the documentation, it seems like pd.json_normalize
will output one row if you give it a dict, but it will output multiple rows if you give it a list of dicts. Since the JSON data from the API looks like it returns the list of articles as a dictionary mapping the item ID to each item, json_string["list"].values()
will produce an iterable over all articles in the list.
Upvotes: 0