Trailing comma in JSON: How to Fix It in PHP
A trailing comma makes json_decode() return null with the message Syntax error. Here is where the comma hides in PHP projects and how to remove it.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →A trailing comma is a comma placed after the last item of a JSON array or object, and the JSON standard does not allow it. In PHP, json_decode() returns null and json_last_error_msg() reports Syntax error.
What “Trailing comma in JSON” means
The parser found a comma that is not followed by another value. RFC 8259 puts commas only between array items or object members, so a comma before a closing bracket or brace breaks the grammar. PHP does not name the comma. It reports a generic Syntax error, and json_decode() returns null unless you pass JSON_THROW_ON_ERROR.
Causes and the fix for each
A comma sits after the last array item.
The array ends with a comma before the closing bracket. RFC 8259 allows commas only between values, so the parser expects another value after the comma and finds a bracket instead. PHP gives up and json_decode returns null. This often happens when someone deletes the last item and leaves the comma on the line above.
Before (fails):{"tags": ["php", "json",]}
After (valid):
{"tags": ["php", "json"]}
A comma sits after the last object member.
The object ends with a comma before the closing brace. The parser reads the comma and then expects a new key in double quotes. It finds a brace, so it stops. This is common after you remove or comment out the final key in a hand-edited config file.
Before (fails):{"id": 7, "name": "Ana",}
After (valid):
{"id": 7, "name": "Ana"}
PHP array habits carried into a JSON file.
PHP accepts a trailing comma after the last element of an array literal, and many style guides encourage it for cleaner diffs. JSON does not follow that rule. When a developer copies a PHP array into a .json file and swaps brackets, the final comma comes along and breaks parsing.
Before (fails):{"roles": ["admin", "editor",], "active": true}
After (valid):
{"roles": ["admin", "editor"], "active": true}
A loop writes a comma after every element.
Code that builds JSON by string concatenation often appends a comma after each item inside a foreach loop. The last item gets a comma too. The output looks fine at a glance, but the parser rejects it. Building a PHP array and calling json_encode avoids the problem.
Before (fails):{"items": [{"id": 1}, {"id": 2},]}
After (valid):
{"items": [{"id": 1}, {"id": 2}]}
A JavaScript or JSON5 config is pasted as JSON.
JavaScript object literals and JSON5 accept trailing commas. Standard JSON does not. A config copied from a JS project, a tsconfig-style file or an editor settings file may carry them into your PHP code, where json_decode follows the stricter RFC 8259 grammar and fails.
Before (fails):{"debug": true, "port": 8080,}
After (valid):
{"debug": true, "port": 8080}
How to fix it in PHP
- Check the error first. Call json_last_error_msg() after json_decode(), or pass JSON_THROW_ON_ERROR and catch JsonException. A trailing comma shows up as Syntax error with code 4.
- Paste the raw string into the Validator. It points to the line and column where the parser stopped, which is usually the closing bracket or brace right after the extra comma.
- Delete the comma that sits directly before a closing ] or }. Check nested arrays and objects too, because one document can hold several.
- If the JSON comes from a file or an API you do not control, paste it into the Repair tool. It removes the stray commas and returns valid JSON you can inspect with the Formatter.
- If your own code builds the JSON, stop concatenating strings. Build a PHP array and call json_encode(), which does not emit trailing commas.
- Decode again with JSON_THROW_ON_ERROR so any remaining problem is raised as an exception instead of a silent null.
<?php
function decodeJson(string $raw): array
{
try {
return (array) json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
// Last resort: remove a comma before ] or }.
// This regex can also change commas inside string values, so log and review.
$fixed = preg_replace('/,\s*([}\]])/', '$1', $raw);
try {
return (array) json_decode($fixed, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e2) {
error_log('Bad JSON: ' . $e2->getMessage());
return [];
}
}
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Syntax error (json_last_error_msg) | Trailing comma, or any other grammar break, in the input | Remove the comma before ] or } and decode again |
| JSON_ERROR_SYNTAX (code 4) | The constant behind the Syntax error message | Validate the string and locate the comma |
| JsonException: Syntax error | Thrown when JSON_THROW_ON_ERROR is set and the input has a trailing comma | Catch the exception, log the raw input, fix the source |
| Expecting property name enclosed in double quotes (Python) | Trailing comma after the last object member | Remove the comma after the last key and value |
| Unexpected token } in JSON (JavaScript) | Trailing comma before a closing brace | Remove the comma before the brace |
Related errors
- Invalid JSON Meaning: What It Is and How to Fix It
- Invalid escape sequence in JSON: How to Fix It in PHP
- Unterminated string in JSON: How to Fix It in PHP
- JavaScript JSON.parse() Errors: What They Mean and How to Fix Them
- Expecting property name enclosed in double quotes (Python Fix)
Key takeaways
- A comma after the last array item or object member is invalid JSON under RFC 8259.
- PHP reports it as Syntax error, and json_decode() returns null by default.
- Use JSON_THROW_ON_ERROR so the failure is visible in your code.
- Build JSON with json_encode() instead of string concatenation.
- Use the Validator to find the comma and the Repair tool to remove it from files you do not control.
FAQ
Does PHP allow trailing commas in JSON?
No. json_decode() follows the RFC 8259 grammar, which allows commas only between values. A trailing comma makes it return null. PHP allows trailing commas in its own array syntax, but that rule does not apply to JSON strings.
Why does json_decode return null with no explanation?
By default json_decode() returns null on failure and does not throw. Call json_last_error_msg() to read the message, or pass JSON_THROW_ON_ERROR to get a JsonException. The flag has been available since PHP 7.3.
Can I strip trailing commas with a regex?
You can, but a pattern like a comma followed by whitespace and a closing bracket may also match text inside string values. Use it only as a logged fallback. Fixing the source or using a repair tool is safer.
Does json_encode ever produce a trailing comma?
No. json_encode() writes valid JSON from a PHP value, so it does not add a comma after the last element. Trailing commas appear when the JSON is written by hand or assembled with string concatenation.
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