python - 包含多个 JSON 文档的文件中的 JSONDecodeError "Expecting property name enclosed in double quotes"

标签 python json

在 Google 上到处查找后,都没有成功找到此问题的解决方案,因此我继续收到以下错误:

JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)

错误发生在我的 Python 文件中的行:row = json.loads(row) 处。 JSON 文件包含 2015-05 的 Reddit 评论中的一部分:

JSON(学习\learning_data\2015\RC_2015-05):

{
  "created_utc": "1430438400",
  "ups": 4,
  "subreddit_id": "t5_378oi",
  "link_id": "t3_34di91",
  "name": "t1_cqug90g",
  "score_hidden": false,
  "author_flair_css_class": null,
  "author_flair_text": null,
  "subreddit": "soccer_jp",
  "id": "cqug90g",
  "removal_reason": null,
  "gilded": 0,
  "downs": 0,
  "archived": false,
  "author": "rx109",
  "score": 4,
  "retrieved_on": 1432703079,
  "body": "\u304f\u305d\n\u8aad\u307f\u305f\u3044\u304c\u8cb7\u3063\u305f\u3089\u8ca0\u3051\u306a\u6c17\u304c\u3059\u308b\n\u56f3\u66f8\u9928\u306b\u51fa\u306d\u30fc\u304b\u306a",
  "distinguished": null,
  "edited": false,
  "controversiality": 0,
  "parent_id": "t3_34di91"
}

*JSON 数据只是我实际拥有的数据的一小部分,而且我无法更改格式。 例如。

{
  "text": "data",
  "text": "data"
}
{
  "text2": "data",
  "text2": "data"
}
{
  "text3": "data",
  "text3": "data"
}
etc...

Python(学习\main.py):

with open("learning_data/{}/RC_{}".format(timeframe.split('-')[0], timeframe), buffering=1000) as f:
for row in f:
    row_counter += 1
    row = json.loads(row)
    body = format_data(row['body'])
    created_utc = row['created_utc']
    parent_id = row['parent_id']
    comment_id = row['name']
    score = row['score']
    subreddit = row['subreddit']       
    parent_data = find_parent(parent_id)

    if score >= 2:
        if acceptable(body):
            existing_comment_score = find_existing_score(parent_id)

JSON 文件已经对所有需要双引号的内容添加了双引号。 如果有其他错误导致此错误 JSONLint.com声称 JSON 是免费的。

我一直在引用 this tutorial 中的代码,其中教程的代码运行良好,没有任何错误(这是根据附加的视频,使用上面链接中的代码,我仍然收到错误)。由于教程使用的是Python 3.5,因此我降级了Python版本,但仍然出现相同的错误。

如果 JSON 已使用双引号且在 JSONLint 之前有效,则此错误的原因是什么? ?

最佳答案

每行一个 JSON 文档流是一种也称为 JSONL 的格式。这与“JSON”不同,“JSON”仅允许一个文件包含一个文档。

您可以通过运行 jq -c . <in.json >out.json 轻松将文件转换为这种格式。 。 jq 是一个用于处理 JSON 和 JSONL 文档的命令行工具; -c标志启用“紧凑”模式,将每个文档放在输出的每一行上。

更简单的是,您可以内联完成此操作,让 Python 代码直接迭代 jq 的输出。 :

import subprocess

with open("learning_data/{}/RC_{}".format(timeframe.split('-')[0], timeframe)) as f:
    p = subprocess.Popen(['jq', '-c', '.'], stdin=f, stdout=subprocess.PIPE)
    for line in p.stdout:
        content = json.loads(line)
        # ...process your line's content here.

关于python - 包含多个 JSON 文档的文件中的 JSONDecodeError "Expecting property name enclosed in double quotes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49661771/

相关文章:

Python GAE : How to return JSON-formated response even when an error occurs?

jquery - 使用 JSON.stringify 在 localStorage 中存储数组

c++ - 我们如何在 boost 属性树中获取对象

python - pandas/json 中的点表示法

python - DRF : only Author can create or update the book permission

python - 使用用户输入从 csv 文件中提取数据

javascript - 如何使用 Selenium WebDriver 滚动自定义滚动条

android - 使用 Python 进行 Unicode URL 编码/解码

Python正则表达式,匹配组跨度(开始和结束)

json - encoding/gob 和 encoding/json 的区别