Missing bracket in JSON: How to Fix It in PHP
PHP reports a missing bracket in JSON as Syntax error or State mismatch. Find the unclosed or mismatched bracket, fix it, and decode with error handling.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →A missing bracket means an opening [ or { has no matching closing ] or }, or the closing one is the wrong type. PHP's json_decode() returns null and json_last_error_msg() reports Syntax error or State mismatch (invalid or malformed JSON).
What “Missing bracket in JSON” means
The parser reached a point where the brackets do not pair up. Every [ needs a ] and every { needs a }, in the right order. PHP has no message with the exact words "Missing bracket". json_decode() returns null, and json_last_error_msg() gives a generic text such as Syntax error when the input ends early, or State mismatch (invalid or malformed JSON) when a closer does not match the opener. With JSON_THROW_ON_ERROR, the same text arrives as a JsonException message.
Causes and the fix for each
An array is opened but never closed.
The ] is missing, so the parser meets } while it still expects ].
Before (fails):{"items": [1, 2, 3}
After (valid):
{"items": [1, 2, 3]}
An object is opened but never closed.
The final } is missing, so the input ends while the object is still open.
Before (fails):{"user": {"id": 7, "name": "Ana"}
After (valid):
{"user": {"id": 7, "name": "Ana"}}
The wrong closing bracket is used.
A ] closes an object or a } closes an array, so the pairs do not match.
Before (fails):{"tags": ["a", "b"}}
After (valid):
{"tags": ["a", "b"]}
The response was cut off before it finished.
A timeout, a size limit or a partial file read can end the text in the middle of a structure.
Before (fails):{"rows": [{"id": 1}, {"id": 2
After (valid):
{"rows": [{"id": 1}, {"id": 2}]}
JSON was built by hand with string concatenation.
A loop or a condition skips one piece, and the closing bracket goes missing in some cases only.
Before (fails):{"list": [{"a": 1}, {"a": 2}
After (valid):
{"list": [{"a": 1}, {"a": 2}]}
How to fix it in PHP
- Print json_last_error_msg() right after json_decode(). Syntax error often points to input that ends early. State mismatch points to a closer that does not match its opener.
- Paste the string into the Validator to see the line and column where the parser stops.
- Count the openers and closers. Indent the text with the Formatter or the Repair tool so each level lines up and the unpaired bracket stands out.
- If the text came from an API or a file, check that it is complete. Compare the length with Content-Length, or check for a timeout or a truncated read.
- If the text is damaged and you cannot regenerate it, run it through the Repair tool, which adds the missing closing brackets, then review the result.
- Build JSON with json_encode() on a PHP array. It writes matching brackets for you, so there is no hand-written bracket to forget.
<?php
function decodeJson(string $raw): array
{
$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) {
$open = substr_count($raw, '{') + substr_count($raw, '[');
$close = substr_count($raw, '}') + substr_count($raw, ']');
error_log(sprintf(
'JSON decode failed: %s (openers=%d, closers=%d, length=%d)',
$e->getMessage(), $open, $close, strlen($raw)
));
throw $e;
}
return is_array($data) ? $data : [];
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Syntax error | The input ends before a bracket is closed, or a bracket is in the wrong place. | Add the missing closing bracket at the end of the open structure. |
| State mismatch (invalid or malformed JSON) | A closing bracket does not match the opener, such as ] closing a {. | Replace the closer with the one that matches its opener. |
| Syntax error on a truncated response | The body was cut off by a timeout or a size limit. | Fetch the data again and check the length before decoding. |
| Syntax error after json_encode output was edited | A template or string replace removed a closing bracket. | Stop editing the encoded text and change the PHP array instead. |
| JsonException: Syntax error | The same failure, thrown because JSON_THROW_ON_ERROR is set. | Catch JsonException and log the raw input length and the message. |
Related errors
- Invalid JSON Meaning: What It Is and How to Fix It
- Trailing comma in JSON: How to Fix It in PHP
- Unterminated string in JSON: How to Fix It in PHP
- JSONDecodeError: Extra data: How to Fix It in Python
- JavaScript JSON.parse() Errors: What They Mean and How to Fix Them
Key takeaways
- A missing bracket leaves an opener without a matching closer, or pairs the wrong types.
- PHP reports it as Syntax error or State mismatch, not as a message that names brackets.
- Truncated responses and hand-built strings are common sources.
- Use JSON_THROW_ON_ERROR and log the raw input length when decoding fails.
- Build output with json_encode() on a PHP array.
FAQ
Does PHP show the exact text "Missing bracket in JSON"?
No. PHP reports Syntax error or State mismatch (invalid or malformed JSON). Developers use the phrase "missing bracket" to describe the cause.
Can PHP tell me which line has the missing bracket?
Not through json_last_error_msg(). It returns only a short message without a line or column. Use the Validator to get the position.
Is a missing bracket the same as a trailing comma error?
No. A trailing comma is a comma before a closing bracket. A missing bracket means the closing bracket itself is absent or wrong. Both can appear as Syntax error in PHP.
Can a tool add the missing bracket for me?
Yes, a repair tool such as the Repair tool can add closing brackets at the end. It guesses where the structure should end, so check that the result holds the data you expect.
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