How to Fix a Python JSONDecodeError in a Config File
A json.decoder.JSONDecodeError means the file is not valid JSON, and the message tells you the line and column where parsing stopped. In config files the cause is almost always a trailing comma, single quotes, a comment, or an unquoted key. Validate the file, correct the syntax, and reload the app.
What the error looks like
An app that loads config.json at startup and fails usually logs something
like this:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 9 column 3 (char 160)
The message has two useful parts: what the parser expected, and where it stopped (line, column, and character offset). The real mistake is usually on that line or at the end of the line just before it.
Newer Python versions word some of these messages differently (for example, they may say "Illegal trailing comma"), but the line and column work the same.
Step 1: validate the file yourself
You do not need to start the app to see the error. Python ships a JSON validator:
python3 -m json.tool config.json
Valid JSON prints back formatted. Invalid JSON prints the same error with the
same position. If jq is installed, jq . config.json does the same job.
Step 2: find the mistake
JSON is much stricter than JavaScript or Python. These are the common mistakes in hand-edited config files:
{
"features": {
"logging": true,
"cache": true,
},
"allowed_origins": [
"https://app.example.com",
]
}
Both , characters before } and ] are trailing commas, and JSON does
not allow them. The corrected file:
{
"features": {
"logging": true,
"cache": true
},
"allowed_origins": [
"https://app.example.com"
]
}
Other rules that break configs:
- Strings and keys must use double quotes.
'debug': falseis invalid. - Keys must be quoted.
{debug: false}is invalid. - No comments.
//and/* */are not part of JSON. - Booleans and null are lowercase:
true,false,null(notTrueorNone).
Correct only the syntax. Keep the same keys and values, so the app behaves the same as before the edit.
Step 3: re-validate and reload
python3 -m json.tool config.json > /dev/null && echo valid
If the app reads the config only once at startup, it keeps the broken state in memory until it restarts. Restart it (or let a file-watching reloader do it), then check the endpoint:
curl -i http://localhost:8080/api/status # expect HTTP 200
Make the app report the problem clearly
A startup loader that catches the error and continues with config = None
produces a 500 on every request and hides the real cause. Fail loudly instead:
import json, sys
try:
with open('config.json') as f:
config = json.load(f)
except json.JSONDecodeError as e:
sys.exit(f'FATAL: config.json is not valid JSON: {e}')
Now the process exits at deploy time with the exact line and column in the log.
Stop bad configs before they reach production
- Run
python3 -m json.toolorjqon every JSON file in CI. - Validate structure, not only syntax, with a JSON Schema check.
- Add a pre-commit hook such as
check-jsonso the mistake never leaves the developer's machine.
A one-character config change can take a service down. Treat config changes with the same review and checks as code changes.
Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.
Start this project - freeWhat you'll practice
- Reading the line and column from a JSONDecodeError
- Validating JSON on the command line with python3 -m json.tool
- Removing trailing commas and other syntax JSON does not allow
FAQ
What does 'Expecting property name enclosed in double quotes' mean?
The parser expected a quoted key but found something else. The usual causes are a trailing comma before a closing brace, a key in single quotes, or an unquoted key.
How do I check if a JSON file is valid?
Run python3 -m json.tool file.json or jq . file.json. Valid JSON prints back formatted; invalid JSON prints an error with the line and column.
Does JSON allow trailing commas or comments?
No. Standard JSON allows neither. If you need comments in config, use a format that supports them, such as YAML or TOML, or a JSON5 parser.
I corrected the file but the app still fails. Why?
Many apps read config only once at startup. Restart the process so it loads the corrected file.
Keep learning
Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.
Start this project - free