How to validate JSON and fix common errors

JSON is intentionally strict. That makes it reliable for APIs, but one small syntax mistake can stop an entire request or configuration file from loading. A validator checks whether text follows the JSON grammar before you use it elsewhere.

Use double quotes for text

Both object keys and string values must use double quotes. The Python-style value {'name': 'Ana'} is not valid JSON. Write {"name": "Ana"} instead. This is the most frequent mistake when moving examples between JavaScript, Python and JSON files.

Remove trailing commas

JSON does not allow a comma after the final item in an object or array. Remove the comma after the final value before parsing. JSON also does not accept comments, even when the surrounding programming language does.

Use JSON literals

JSON recognizes true, false and null, all in lowercase. Python's True, False and None are not valid JSON. These differences are small but matter whenever text crosses a language boundary.

A silent failure I ran into: PHP's json_decode does not always tell you it failed

This is the one that actually cost me debugging time on my own backend. Most of my API endpoints share this helper:

function json_body() {
    $raw = file_get_contents('php://input');
    $data = json_decode($raw, true);
    return is_array($data) ? $data : [];
}

That looks safe — if decoding fails, you get an empty array instead of a crash. But it means a genuinely malformed request body and an empty or missing request body produce the exact same result: []. When a client bug sent a truncated JSON body, every endpoint using this helper just silently treated it as "no fields sent" and returned a generic validation error, instead of a clear "your JSON is invalid" message. I spent longer than I should have checking the wrong endpoint before realizing the request body itself was the problem.

In PHP specifically, json_decode() returns null both when the input is invalid JSON and when the input is the valid JSON literal null. The only reliable way to tell them apart is checking json_last_error() right after decoding:

$data = json_decode($raw, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    // the body was not valid JSON at all
}
Takeaway: if your API validation errors feel misleading ("missing field" when you know you sent it), check whether your JSON parsing layer is swallowing a decode failure before you trust the field-level error.

Validate before debugging the application

When an API returns a parsing error, validate the exact response or request body first. Then check expected field names and data types. A syntactically valid document can still fail an API schema if a field expected to be a number is sent as text.

Use the JSON to Python converter to inspect a valid document, then read our JSON to Python guide for the next step.