0

So, I am trying to load a JSON file into Python and I am getting the following error:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes

My JSON file (the beginning thereof) looks like this:

{ 
    //Deploy to http://localhost:9200/_template/cwmp_genieacs
    "!deploy_to": "cwmp_genieacs",
    "index_patterns" : ["cwmp_genieacs_*"],
    "version": 180201, // Increase this with every release
    "order" : 9999999, // Decrease this with every release
    "mappings": {
        "cwmp_genieacs": {
            "properties": {
                "@timestamp": { "type": "date", "format": "strict_date_optional_time||epoch_millis" },

                "FactoryReset": {"type": "date"},
                "InternetGatewayDevice.Capabilities.PerformanceDiagnostic.DownloadTransports": {"type": "keyword"},
                "InternetGatewayDevice.Capabilities.PerformanceDiagnostic.UploadTransports": {"type": "keyword"},
                "InternetGatewayDevice.CrashDiagnostics.Action": {"type": "keyword"},

Now, I understand what the problem is. The first few lines aren't in the correct JSON format. When I delete them and start at "mappings" it works fine. But I need to load the JSON file without editing the file. So, are there any solutions?

For the first line (the one that starts with "//") I could maybe use the startswith() function but I don't know what to do with the two lines that start with "version": 180201 and "order" : 9999999.

I am using Python 3.6.

Jan Pisl
  • 1,043
  • 2
  • 14
  • 29
  • 1
    Read the file line by line, remove all characters after and including `//` on each line then load the resulting string into a JSON (or python dict, it's easier to work with)? – BoboDarph Mar 01 '18 at 14:34
  • 1
    Do you know exactly on what lines you what lines are incorrect? If so, you can just read JSON file as string, delete incorrect lines and import JSON from string. – Kyrylo Mar 01 '18 at 14:35
  • 2
    I think you main issue is that you cannot have comments in json, so what you are dealing with is the fact your 'JSON' isn't actually JSON. The only thing you could do is treat your whole input as a string, then remove any // and replace them with something like "deploy_comment": "Deploy to http://localhost:9200/_template/cwmp_genieacs", "version_comment": "Increase this with every release", and then parse your new string as JSON. – dave Mar 01 '18 at 14:35

0 Answers0