Normalise JSON response
I have the following code
import requests
import json
import pandas as pd
url = "https://www.pedrofonseca.eu/trabalho/projectoslei/api/projects/getByLegislature/XIV"
response = requests.get(url)
json = response.json()
df_final= pd.json_normalize(
json,
record_path =['authors'],
meta=['id', 'typeid'],
errors='ignore'
)
This returns a KeyError on authors
Basically I'm having difficulties in extracting the json data, from its three levels, onto a dataframe
Any help is much appreciated.
Solution 1:
This error is happening because some items don't have author properties, we have to filter out items with no authors in the list my brother.
import requests
import json
import pandas as pd
url = "https://www.pedrofonseca.eu/trabalho/projectoslei/api/projects/getByLegislature/XIV"
response = requests.get(url)
json = response.json()
print(len(json))
def find_item_with_authors(json):
output = []
for item in json:
if 'authors' in item:
output.append(item)
return output
items_with_authors = list(filter(find_item_with_authors, json))
print(len(items_with_authors))
df_final= pd.json_normalize(
items_with_authors,
record_path =['authors']
)