Need some expert help on how to debug this and force a double quotes where missing, so that i can get a successful response. It is throwing error here df = pd.io.json.json_normalize(rt.json(), record_path='offers'). The josn looks fine i have staged into csv, so not sure where the issue is
my Code
import requests
import csv
import json
import pandas as pd
from pandas.io.json import json_normalize
url = "https://authentication.skiapi.com/access_token"
payload = {
"client_id": "00c7fcf******",
"client_secret": "7676cd5a********",
"grant_type": "client_credentials"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
r = response.json()
access_token = r.get('access_token')
print(response.json()['access_token'])
uri = "https://private-anon-73f9ac5d87-slinksmerchantapi.apiary-mock.com/v4/publisher/12633/offers?country=US"
headers = {'Authorization': access_token,'Content-Type': "application/json"}
rt = requests.get(uri, headers=headers)
df = pd.io.json.json_normalize(rt.json(), record_path='offers') ###problem is here
#print(df)
df.to_csv(r"C:\\Users\ral\Downloads\\offers.csv", index=False)
My Sample JSON
{
"has_more": true
"offers": [{
"coupon_code": null
"terms": null
"description": "40% Off Comforters & Other Cool Bedding Stuff Online at BoxLunch! Stock up on select blankets and bedding online only for a limited time -- See site for details. Valid 3/3-3/5"
"offer_starts": "2017-03-03 08:00:00"
"title": "40% Off Comforters & Other Cool Bedding Stuff Online at BoxLunch!"
"url": "http://www.boxlunch.com/home/bedroom/?soffer=152034"
"merchant_details": {
"domain": "boxlunchgifts.com"
"verticals": []
"country_code": null
"id": 393756
"metadata": {}
"favourite": false
"partner_type": null
"merchant_id": 383288
"advertiser_id": 123456
"name": "BoxLunch"
"countries": []
"domains": [
"boxlunchgifts.com"
"boxlunch.com"
]
}
"offer_type": "sale"
"id": 152034
"offer_ends": "2017-03-05 08:00:00"
}]
"last_val": 152034
"next_val": 152032
"num_returned": 1
}
The JSON is not fine - as per comments commas are missing plus true should be True and null should be None in python
Additionally you are using deprecated interface to json_normalize
what does json.dumps(rt.json(), indent=2)
return? Valid json?
patched json
json = {
"has_more": True,
"offers": [{
"coupon_code": None,
"terms": None,
"description": "40% Off Comforters & Other Cool Bedding Stuff Online at BoxLunch! Stock up on select blankets and bedding online only for a limited time -- See site for details. Valid 3/3-3/5",
"offer_starts": "2017-03-03 08:00:00",
"title": "40% Off Comforters & Other Cool Bedding Stuff Online at BoxLunch!",
"url": "http://www.boxlunch.com/home/bedroom/?soffer=152034",
"merchant_details": {
"domain": "boxlunchgifts.com",
"verticals": [],
"country_code": None,
"id": 393756,
"metadata": {},
"favourite": False,
"partner_type": None,
"merchant_id": 383288,
"advertiser_id": 123456,
"name": "BoxLunch",
"countries": [],
"domains": [
"boxlunchgifts.com",
"boxlunch.com"
]
},
"offer_type": "sale",
"id": 152034,
"offer_ends": "2017-03-05 08:00:00"
}],
"last_val": 152034,
"next_val": 152032,
"num_returned": 1
}
pd.json_normalize(json, record_path="offers")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.