【问题标题】:Create nested json file创建嵌套的 json 文件
【发布时间】:2020-02-19 08:55:02
【问题描述】:

我想创建一个可以用作配置文件的json文件。我有来自多家公司的不同文件,它们以不同的列名报告相同的信息。

我想使用 json 文件中提供的信息来运行 python 脚本,以将来自所有文件和公司的所有信息合并到一个主文件中。

结构如下:

{"companies":
    {"company1": [
        {"path": "C:/USER/Path/Company1/",
         "files": [
            {
                {"_CO": {"ID": "ID", "Report Number": "Report_Number"}},
                {"_TR": {"ID": "Trade_Ident", "Report Number": "Number of Report"}},    
            },
         ],
        },
    ],
    },

    {"company2": [
        {"path": "C:/USER/Path/Company2/",
         "files": [
            {
                {"_CO": {"ID": "Identification", "Report Number": "Report-Number"}},
                {"_TR": {"ID": "Ident", "Report Number": "NumberReport"}},  
            },
         ],
        },
    ],
    },
},

但是,我在尝试读取 python 中的 .json 时收到以下错误。

json.decoder.JSONDecodeError:期望属性名称包含在 双引号:第 6 行第 5 列(char 90)

读取我使用的文件:

import json

path = "/user_folder/USER/Desktop/Data/"

file = "ConfigFile.json"

with open(path+file) as f:
    my_test = json.load(f)

感谢您的帮助,因为我无法找出文件结构中的错误。

【问题讨论】:

    标签: python json file nested


    【解决方案1】:

    您收到错误是因为您的json 文件格式不正确,因此调用json.load() 将引发JSONDecodeError

    您的json 结构应如下所示,

    {
        "companies": {
            "company1": [
                {
                    "path": "C:/USER/Path/Company1/",
                    "files": [
                        {
                            "_CO": {
                                "ID": "ID",
                                "Report Number": "Report_Number"
                            }
                        },
                        {
                            "_TR": {
                                "ID": "Trade_Ident",
                                "Report Number": "Number of Report"
                            }
                        }
                    ]
                }
            ],
            "company2": [
                {
                    "path": "C:/USER/Path/Company2/",
                    "files": [
                        {
                            "_CO": {
                                "ID": "Identification",
                                "Report Number": "Report-Number"
                            }
                        },
                        {
                            "_TR": {
                                "ID": "Ident",
                                "Report Number": "NumberReport"
                            }
                        }
                    ]
                }
            ]
        }
    }
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      您有一些没有键的对象(带有花括号的对象),例如在

      {
          {"_CO": {"ID": "ID", "Report Number": "Report_Number"}}, ...
      
      

      JSON 中的对象是键值对。去掉外面的大括号就可以了。

      你可以像this one一样使用一些在线的JSON格式化程序/验证器,它会很容易地指出问题。否则,您可以为您的编辑器使用一些 JSON linter。它只是为你工作,还改善了缩进:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-02
        • 1970-01-01
        • 2022-01-23
        • 2013-06-07
        • 1970-01-01
        • 1970-01-01
        • 2023-02-23
        相关资源
        最近更新 更多