Maeaex1
Maeaex1

Reputation: 755

Create nested json file

I want to create a json file that can be used as a configuration file. I have different files from multiple companies that report the same information with different column names.

I want to use the information provided in the json file to run a python script to consolidate all the information from all files and companies in one master file.

The structure looks like follows:

{"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"}},  
            },
         ],
        },
    ],
    },
},

However, I receive the following error when trying to read the .json in python.

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 6 column 5 (char 90)

To read the file I use:

import json

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

file = "ConfigFile.json"

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

Any help appreciated, as I can't figure out my mistake in the file structure.

Upvotes: 2

Views: 1555

Answers (2)

Shubham Sharma
Shubham Sharma

Reputation: 71687

You're getting error because your json file is incorrectly formatted and thus calling json.load() will raise an JSONDecodeError.

Your json structure should look like,

{
    "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"
                        }
                    }
                ]
            }
        ]
    }
}

Hope it helps you!

Upvotes: 1

Maura Pintor
Maura Pintor

Reputation: 306

You have some object (the ones with curly braces) without keys, for example in

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

Objects in JSON are key-value pairs. Just remove the external set of braces and it should be ok.

You can use some online JSON formatter/validator just like this one, and it will easily point out the problem. Otherwise, you can use some JSON linter for your editor. It just does the work for you and also improves indentation :)

Upvotes: 1

Related Questions