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.

Start this project - freeLive cloud workspace in your browser · lands on your portfolio
DevOps Engineerpythonjsonflask

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:

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

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 - free

What you'll practice

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

Restore a Misplaced Linux Config FileDevOps projectRecover a Crashed Linux ServiceDevOps projectFix the CRLF Line Endings Breaking start.shDevOps projectDevOps roadmapStep by step to hiredDevOps interview questionsSTAR answersAll DevOps projectsProjects hub

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
Start this project - free