Got a problem with this API request. Actually two problems. The JSON file of the type I'm accessing can be seen here: https://api.nfz.gov.pl/app-umw-api/plans/55d957ea-3640-44e7-7998-4dc35c3d0c23?page=1&limit=15&format=json&api-version=1.2 My plan_list1 is just a file with a long list of codes like this: 55d957ea-3640-44e7-7998-4dc35c3d0c23?page=1&limit=15&format=json&api-version=1.2
I'm trying to get info from branches of this JSON:
- from 'meta' I'd like to get the 'count'
- from 'data/header' - all of it
- from 'data/agreement plan' - all the 'attributes' and crucially the 'id'
I'm trying to use a version of a code that worked on other similar API methods from the same source, but there we had dictionaries within lists within dictionaries, here it should be more straightforward.
However, I don't really understand this and various attempts like this one here yield errors saying: "all_data.append({**a["header"], "plan-number": a["agreement-plan"]['id']}) TypeError: string indices must be integers"
import requests
import pandas as pd
from plans_list1 import plans
baseurl = 'https://api.nfz.gov.pl/app-umw-api/plans/'
all_data = []
for plan in plans:
api_url = ''.join([baseurl, plan])
def main_request(api_url):
r = requests.get(api_url)
return r.json()
for page in range(0, 1):
data = requests.get(api_url.format(page)).json()
for a in data["data"]:
all_data.append({**a["header"], "plan-number": a["agreement-plan"]['id']})
df = pd.DataFrame(all_data)
df.to_csv('2018-plans-detail.csv', encoding='utf-8-sig', index=False)
print(get_pages(p_number))
for a in data["data"]Iterating over a dictionary gives you the keys, which are strings. So in this loop,ais a string. But it seems you are expecting it to be a dictionary.data["header"],data["agreement-plan"], etc.