1

I'm trying to configure my Python logging module using logging.config.dictConfig()

However, my JSON file appears to be corrupt even though it works as a Python dict with false changed to False, so I don't think it's a syntax error.

My code says:

import json

with open('logging_config.json') as f:
    d = json.load(f)

My logging_config.json file is:

{
  "version": 1,
  "disable_existing_loggers": false,
  "formatters": {
    "standard": {
      "format": "[%(levelname)s] %(name)s: %(message)s"
    },
  },
  "handlers": {
    "default": {
      "level": "DEBUG",
      "formatter": "standard",
      "class": "logging.StreamHandler",
    },
  },
  "loggers": {
    "": {
      "handlers": ["default"],
      "level": "INFO",
      "propagate": true
    },
    "myfunc": {
      "handlers": ["default"],
      "level": "DEBUG",
      "propagate": false
    },
  }
}

When I run that simple program, I get an error: json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 8 column 3 (char 156)

What could be causing the JSON decoder to fail?

Thanks in advance for any help.

3
  • The JSON parser is expecting a certain type of value for each key. A string is one accepted value, an integer another. You're trying to insert a boolean, but there is a difference between upper and lowercase for booleans. Only one is a valid boolean. Here's some documentation Commented Aug 22, 2017 at 16:01
  • 2
    @ajoseps that isn't the issue. The issue is you have an extraneous comma, so JSON expects a key. How was this JSON generated? It certainly wasn't by the python json module Commented Aug 22, 2017 at 16:03
  • It used to be a Python dict which I converted manually. Python forgives the extra commas but json doesn't Commented Aug 22, 2017 at 16:14

3 Answers 3

8

You have multiple extraneous commas which will break the JSON deserialization.

Here's your original JSON with every extraneous comma pointed out:

{
  "version": 1,
  "disable_existing_loggers": false,
  "formatters": {
    "standard": {
      "format": "[%(levelname)s] %(name)s: %(message)s"
    }, # <--- extraneous comma
  },
  "handlers": {
    "default": {
      "level": "DEBUG",
      "formatter": "standard",
      "class": "logging.StreamHandler", # <--- extraneous comma
    }, # <--- extraneous comma
  },
  "loggers": {
    "": {
      "handlers": ["default"],
      "level": "INFO",
      "propagate": true
    },
    "myfunc": {
      "handlers": ["default"],
      "level": "DEBUG",
      "propagate": false
    }, # <--- extraneous comma
  }
}

Removing those, this is now a valid JSON document:

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "standard": {
            "format": "[%(levelname)s] %(name)s: %(message)s"
        }
    },
    "handlers": {
        "default": {
            "level": "DEBUG",
            "formatter": "standard",
            "class": "logging.StreamHandler"
        }
    },
    "loggers": {
        "": {
            "handlers": ["default"],
            "level": "INFO",
            "propagate": true
        },
        "myfunc": {
            "handlers": ["default"],
            "level": "DEBUG",
            "propagate": false
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, Python forgives these extra commas. I missed them when converting the Python to json. Thanks
2

As others already answered, there are some excess commas in your JSON.

You could feed your JSON to https://jsonlint.com/ ; it tends to give more useful help messages.

Comments

1

This happens because your JSON is invalid. this one JSON is valid. I removed extra commas from your JSON. You can use any JSON validator to fix it.

{
  "version": 1,
  "disable_existing_loggers": false,
  "formatters": 
  {
    "standard": 
    {
      "format": "[%(levelname)s] %(name)s: %(message)s"
    }
  },
  "handlers": 
  {
    "default": 
    {
      "level": "DEBUG",
      "formatter": "standard",
      "class": "logging.StreamHandler"
    }
  },
  "loggers": 
  {
    "": 
    {
      "handlers": ["default"],
      "level": "INFO",
      "propagate": true
    },
    "myfunc": 
    {
      "handlers": ["default"],
      "level": "DEBUG",
      "propagate": false
    }
  }
}

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.