I have this code that returns a json response.
import requests
url = "https://usda.library.cornell.edu/api/v1/publication/findAll"
payload = {}
headers = {
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjE0NDc1fQ.YpppSpjBnGfHv8DBEiWCrmv-KgtF3MLzniHF89hW9L8'
}
response = requests.request("GET", url, headers=headers, data=payload)
I am trying to use json_normalize as shown here to get a pandas data frame.
data = response.text
df = json_normalize(data)
print (df)
Getting NotImplementedError:
What am I doing wrong?
>Solution :
Use response.json(), not response.text in pd.json_normalize:
import requests
import pandas as pd
url = "https://usda.library.cornell.edu/api/v1/publication/findAll"
payload = {}
headers = {
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjE0NDc1fQ.YpppSpjBnGfHv8DBEiWCrmv-KgtF3MLzniHF89hW9L8'
}
response = requests.request("GET", url, headers=headers, data=payload)
df = pd.json_normalize(response.json())
print(df)
Prints:
id agency title contact_organization contact_email keywords frequency status subscribable resource_type subject description identifier agency_acronym upcoming_releases division_acronym related_publication
0 df65v784d [Foreign Agricultural Service] [Tobacco: World Markets and Trade] [Foreign Agricultural Service] [Press@fas.usda.gov] [cigarettes, burley tobacco, tobacco, flue-cured tobacco, tariffs] [Monthly] [Inactive] No [Report] [Crops and Crop Products:Tobacco] [This monthly report includes data on annual and monthly U.S. and global trade, production, and consumption of manufactured and unmanufactured tobacco on a commodity basis and a country by country basis.\n] [Tobacco] [FAS] NaN NaN NaN
1 5x21tf405 [Foreign Agricultural Service] [Organics: World Markets and Trade] [Foreign Agricultural Service] [Press@fas.usda.gov] [organic foods, imports, exports, organic] [Not Specified] [Active] No [Report] [Animals and Animal Products:Specialty Animals and Animal Products, Crops and Crop Products:Specialty Crops and Crop Products] [This report includes data on U.S. trade on a country by country basis and on a commodity basis in labeled organic foods. Also included are analysis of developments affecting trade in organics.\n] [organics] [FAS] NaN NaN NaN
2 k930bx013 [National Agricultural Statistics Service] [Alfalfa Seed] [National Agricultural Statistics Service] [nass@nass.usda.gov] [alfalfa, yields, seedling production] [Annually] [Inactive] No [Report] [Crops and Crop Products:Grains and Oilseeds] [This publication reported on the alfalfa seed crop, including area harvested, yield and production, as well as carryover and supply. ] [AlfaSeed] [NASS] NaN NaN NaN
...