How can I convert the following list of dicts (json output) to a pandas DataFrame. I tried
res = {}
for d in list_of_dict:
res.update(d)
It gives me the error:
ValueError: dictionary update sequence element #0 has length 33; 2 is required
Example JSON output, needed converted to DataFrame.
{
"PlanCoverages": [
{
"PlanId": 65860,
"FormularyId": 61855,
"PlanName": "CVS Caremark Performance Standard Control w/Advanced Specialty Control",
# :
"OverTheCounter": false
},
{
"PlanId": 69549,
"FormularyId": 63811,
"PlanName": "CVS Caremark Performance Standard Opt-Out w/ Advanced Specialty Control ",
# :
"OverTheCounter": false
} ]
}
Here is my full code. It connects to an API, and scraps information on pharmaceuticals. I need the PlanCoverages of 1330 plans.
import requests
import pandas as pd
from pandas.io.json import json_normalize
import json
headers = {
'Accept': '*/*',
'X-Requested-With': 'XMLHttpRequest',
'Access-Token': 'H-oa4ULGls2Cpu8U6hX4myixRoFIPxfj',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36',
'Is-Session-Expired': 'false',
'Referer': 'https://formularylookup.com/',
}
response = requests.get('https://formularylookup.com/Formulary/Coverage/Controller?ProductId=237171&ProductName=Rybelsus&ControllerId=884&ChannelId=1&StateId=all&DrugTypeId=3&Options=PlanCoverages', headers=headers)
df = response.json()
df_normal = json_normalize(df)["PlanCoverages"]#["ControllerCoverages"]
#dff = pd.DataFrame(df_normal)
#dff = json.dumps(df, indent=4, sort_keys=False)
res = {}
for d in df_normal:
res.update(d)
print(res)
Ideal output is, 1 row per plan. So a total of 1330 rows.