2

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.

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.