json.decoder.JSONDecodeError 错误 python

问题描述 投票:0回答:1

我有一个自动化程序,可以运行 json 对象并将其传递给 python 脚本。

我的目标是读取 json 并将其转换为字典。

我的 JSON 如下所示:

{
    "Items": [
        {
            "Name": "foo",
            "File": "\\\\files\\foo\\foo.csv"
        },
        {
            "Name": "bar",
            "File": "\\\\files\\bar\\bar.csv"
        },
        {
            "Name": "baz",
            "File": "\\\\files\\baz\\baz.csv"
        }
    ]
}

我需要它看起来像这样:

 {
   "foo" : "\\\\files\\foo\\foo.csv",
   "bar" : "\\\\files\\bar\\bar.csv",
   "baz" : "\\\\files\\baz\\baz.csv"
 }

我在这段代码中遇到此错误:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 4 (char 6)
 if len(sys.argv) > 1:
        d = json.loads(sys.argv[1])
        print(d)
python json
1个回答
0
投票
import json

data = json.loads(original_json)

converted_data = {item["Name"]: item["File"] for item in data["Items"]}

converted_json = json.dumps(converted_data, indent=4)

print(converted_json)

Results from above Code

© www.soinside.com 2019 - 2024. All rights reserved.