【问题标题】:JSONDecodeError while merging JSON合并 JSON 时出现 JSONDecodeError
【发布时间】:2020-07-25 02:48:36
【问题描述】:

我正在尝试将多个 .json 合并到一个 json 文件中

代码:

import glob, json
read_files = glob.glob("*.json")
output_list = []
for f in read_files:
    with open(f, "rb") as infile:
        output_list.append(json.load(infile))
all_items = []
for json_file in output_list:
    all_items += json_file['value']
textfile_merged = open('merged.json', 'w')
json.dump({ "value": all_items }, textfile_merged)
textfile_merged.close()

我收到以下错误

> Traceback (most recent call last):   File "D:\JSON\merge.py", line 7,
> in <module>
>     output_list.append(json.load(infile))   File "C:\Program Files (x86)\Python38-32\lib\json\__init__.py", line 293, in load
>     return loads(fp.read(),   File "C:\Program Files (x86)\Python38-32\lib\json\__init__.py", line 357, in loads
>     return _default_decoder.decode(s)   File "C:\Program Files (x86)\Python38-32\lib\json\decoder.py", line 337, in decode
>     obj, end = self.raw_decode(s, idx=_w(s, 0).end())   File "C:\Program Files (x86)\Python38-32\lib\json\decoder.py", line 355, in
> raw_decode
>     raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
> 0)

【问题讨论】:

  • 其中一个文件不是有效的 JSON。

标签: python json glob


【解决方案1】:

您的其中一个 JSON 文件似乎是空的(没有 {})或格式不正确。

确保你的空 json 文件有 {} 它必须工作。 祝你好运!

【讨论】:

  • 谢谢,我在一个文件中发现 } 没有关闭
猜你喜欢
  • 2018-04-25
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2021-02-14
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
相关资源
最近更新 更多