Invalid JSON: What It Means and How to Fix It in PHP
Invalid JSON in PHP means json_decode could not parse the text. Learn the common causes, how to read the error, and how to fix the input.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →Invalid JSON means the text breaks the JSON grammar, so PHP's json_decode returns null and json_last_error_msg() reports Syntax error. Find the first bad character, fix it, and decode again.
What “Invalid JSON” means
Invalid JSON means the parser hit text that the JSON grammar does not allow. PHP has no message called Invalid JSON. Your app or framework adds it after json_decode returns null and json_last_error() is not zero. The usual underlying message is Syntax error. The parser stops at the first problem and does not say which line it was on.
Causes and the fix for each
A trailing comma follows the last item.
RFC 8259 does not allow a comma after the last member of an object or array. PHP rejects it with a syntax error. Editors and code generators often leave one behind after a line is deleted.
Before (fails):{"a": 1,}
After (valid):
{"a": 1}
Strings or keys use single quotes.
The JSON standard allows only double quotes around strings and object keys. PHP's json_decode treats single quotes as a syntax error and returns null. This often happens when someone pastes a PHP array or a JavaScript object literal into a JSON field. Replace each single quote that delimits a string or key with a double quote.
Before (fails):{'name': 'Ana'}
After (valid):
{"name": "Ana"}
An object key has no quotes.
Object keys must be strings in double quotes. Bare keys work in JavaScript source but not in JSON.
Before (fails):{name: "Ana"}
After (valid):
{"name": "Ana"}
A closing bracket or brace is missing or mismatched.
Every opening bracket needs a matching closer of the same kind. Truncated responses and bad copy-paste often cut the end off.
Before (fails):{"a": [1, 2}
After (valid):
{"a": [1, 2]}
The text contains comments.
JSON has no comment syntax. Config files written in JSON-with-comments formats fail in json_decode.
Before (fails):{"a": 1 /* note */}
After (valid):
{"a": 1}
How to fix it in PHP
- Check the error first: call json_last_error_msg() right after json_decode, or pass JSON_THROW_ON_ERROR and catch JsonException.
- Print the raw input before decoding. Check for an empty string, an HTML error page, or a truncated body.
- Paste the text into the Validator to find the first bad character, or into the Formatter to see where the structure breaks.
- Fix the usual suspects: trailing commas, single quotes, unquoted keys, comments, and missing brackets.
- If the text came from a model or a log and has many faults, run it through the Repair tool and review the result.
- Remove a UTF-8 byte order mark if the input starts with one, then decode again.
<?php
function decodeJson(string $raw): array
{
// Remove a UTF-8 BOM if present.
$raw = preg_replace('/^\xEF\xBB\xBF/', '', $raw);
$raw = trim($raw);
if ($raw === '') {
throw new InvalidArgumentException('Empty JSON input');
}
try {
$data = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
error_log('Invalid JSON: ' . $e->getMessage() . ' | start: ' . substr($raw, 0, 200));
throw new InvalidArgumentException('Invalid JSON: ' . $e->getMessage(), 0, $e);
}
if (!is_array($data)) {
throw new InvalidArgumentException('Expected a JSON object or array');
}
return $data;
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Syntax error | Trailing comma, single quotes, unquoted key, comment, or missing bracket | Find the first bad character and correct it |
| Malformed UTF-8 characters, possibly incorrectly encoded | Input is not valid UTF-8, often Latin-1 text | Convert with mb_convert_encoding before decoding |
| Control character error, possibly incorrectly encoded | Raw newline or tab inside a string | Escape it as \n or \t |
| Maximum stack depth exceeded | Nesting is deeper than the depth argument | Raise the depth argument or check the input |
| State mismatch (invalid or malformed JSON) | Brackets do not match, such as ] closing a { | Match each opener with the right closer |
Related errors
- Single quotes are not valid JSON: How to Fix It in PHP
- Trailing comma in JSON: How to Fix It in PHP
- Missing bracket in JSON: How to Fix It in PHP
- Unterminated string in JSON: How to Fix It in PHP
- Invalid JSON Meaning: What It Is and How to Fix It
Key takeaways
- Invalid JSON in PHP usually shows up as null plus the message Syntax error.
- The parser stops at the first problem and gives no line number.
- Trailing commas, single quotes, unquoted keys, comments, and missing brackets cause most cases.
- Use JSON_THROW_ON_ERROR so failures are not silent.
- Log the start of the raw input when decoding fails.
FAQ
Why does json_decode return null?
It returns null when the text is not valid JSON, or when the text is the JSON value null. Check json_last_error() to tell them apart.
How do I get the error message in PHP?
Call json_last_error_msg() after json_decode, or pass JSON_THROW_ON_ERROR and read the message from the JsonException.
Does PHP tell me the line of the error?
No. The standard json_decode reports an error code and message, not a line or column. Use a validator to locate the position.
Is a PHP array string valid JSON?
No. A PHP array literal with single quotes or arrow syntax is not JSON. Use json_encode to produce JSON from an array.
Can a tool fix invalid JSON for me?
A repair tool can fix common faults such as trailing commas and single quotes. Review the output, because repairs are guesses about intent.
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →The JSON Medic newsletter
One JSON error and its fix, weekly, plus new guides and tools. Free.
We send a confirmation link first. Unsubscribe with one click in any mail. See the privacy policy.
Related Articles