【问题标题】:Python: JSONDecodeError: Expecting property name enclosed in double quotesPython:JSONDecodeError:期望用双引号括起来的属性名称
【发布时间】:2020-07-09 01:39:52
【问题描述】:

需要一些专家的帮助来解决这个问题并在缺失的地方强制使用双引号,这样我才能得到成功的响应。它在这里抛出错误 df = pd.io.json.json_normalize(rt.json(), record_path='offers')。 josn 看起来不错,我已经上演到 csv,所以不确定问题出在哪里

我的代码

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)

我的 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
}

【问题讨论】:

  • @tdelaney ..没办法..我确实擦洗了它们..但是我对帖子进行了编辑以避免这种混乱谢谢
  • 这个示例 json 是在哪里形成的?这可能是我自己缺乏知识,但我注意到的第一件事是没有逗号分隔元素。我认为这样的事情会导致 JSON 解码器出现问题。更多关于该示例 json 的上下文可能会有所帮助。
  • @Eddie 这是一个 API 调用,响应是 JSON。我们可以在这里使用 JSON 格式化函数吗?确保它是正确的 JSON 格式
  • json 无效。至少缺少逗号。你可以json.dump(rt.json(), open("rt.json", "w"), indent=4) 然后发布。此外,确切的 python 回溯很有用,因此我们可以看到失败的位置。
  • 想一想,如果json解码失败,rt.contentrt.raw.read()可能会更好。

标签: python pandas jupyter-notebook


【解决方案1】:

JSON 不正确 - 根据 cmets 缺少逗号加上 true 应该是 True 并且 null 应该是 None在python中

此外,您正在使用已弃用的接口 json_normalize

json.dumps(rt.json(), indent=2) 返回什么?有效的json?

已修补的 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")

【讨论】:

  • 当我执行这个 json.dumps(rt.json(), indent=2) 时,我得到 JSONDecodeError: Expecting property name included in double quotes: line 26 column 9 (char 1006)
  • 好的 - 这是您问题的真正根源。 API 未提供有效的 JSON。如果它是内部服务,您应该让 API 提供商修复 API。外部尝试针对它提出错误报告
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 2019-04-24
相关资源
最近更新 更多