Need assistance only pulling / filtering "results" from json data as shown below
[
{
"reportSection": "Report Detail",
"reportSections": [
"Report Header",
"Report Detail"
],
"stats": {
"returnedRows": 22,
"userAllowedRows": 100000,
"totalRows": 22
},
"results": [
{
"report_date": "05/31/2023",
"report_begin_date": "05/31/2023",
"report_end_date": "05/31/2023",
"published_date": "05/31/2023 13:55:06",
"office_name": "Minneapolis, MN",
"office_city": "Minneapolis",
"office_state": "MN",
"market_type": "Point of Sale - Grain",
"market_location_name": "Grain - South Dakota",
"market_location_city": "Sioux Falls",
"market_location_state": "SD",
"slug_id": 3186,
"slug_name": "AMS_3186",
"report_title": "South Dakota Daily Grain Bids",
"grp": "Grains",
"cat": "Coarse",
"commodity": "Soybeans",
"location_State": null,
"location_City": null,
"trade_loc": "Central",
"class": null,
"grade": "US #1",
"protein": null,
"application": null,
"pkg": "bulk",
"desc": null,
"quote_type": "Basis",
"delivery_point": "Country Elevators",
"conventional": "Conventional",
"basis_unit": "¢/Bu",
"sale Type": "Bid",
"basis Min": 20,
"basis Min Futures Month": "August (Q)",
"basis Max": 30,
"basis Max Futures Month": "August (Q)",
"basis Min Change": 0,
"basis Min Direction": "UNCH",
"basis Max Change": 0,
"basis Max Direction": "UNCH",
"price_unit": "$ Per Bushel",
"price Min": 12.3775,
"price Max": 12.4775,
"price Min Change": 0.0175,
"price Min Direction": "DN",
"price Max Change": 0.0175,
"price Max Direction": "DN",
"avg_price": 12.4108,
"avg_price_year_ago": 16.3867,
"freight": "Delivered",
"trans_mode": "Truck",
"current": "Yes",
"delivery_start": null,
"delivery Start Half": null,
"delivery_end": null,
"delivery End Half": null
}
]
}
]
Only been successful at querying API and pulling back json data file in Python, but trying to learn how to pull out only a specific section of data and read to a data frame. Example, data where Report Section = ‘Report Detail’ and bring back all ‘results’ rows
Below is python code attempted so far:
datapull = response_API.text
data = pd.read_json(datapull)
def section_results(record):
section = record.get('reportSection', 'Report Header')
result = section.get('results', None)
print([section_results(r) for r in data])
#data = pd.read_json(datapull)
#df = pd.Series(validate_record_schema(data)).to_frame()
#print (df)
Error I am getting:
enter image description here
2
Answers
Thanks for the help and I now have it working:
How would one suggest best way to read the list of 'reportSections' so that I can filter data for each section?
What you have is string with Json data. You can use
json
module to parse it and then filter it to pandas DataFrame:Prints: