How to Fix Invalid JSON Before Sending an API Request
A 400 Bad Request from an API that expects JSON almost always means the JSON is malformed. The API can't parse it, so it rejects the whole request. The fix is to find and correct the syntax error before resending.
Trailing commas
The most common JSON error. A trailing comma appears after the last element in an array or the last key-value pair in an object:
// Invalid — trailing comma after "dark"
{"theme": "dark",}
// Invalid — trailing comma after 3
[1, 2, 3,]
// Valid
{"theme": "dark"}
[1, 2, 3]
This is the number one mistake because JavaScript allows trailing commas (since ES5) and many languages tolerate them, creating habits that carry over into JSON where they're strictly prohibited.
Wrong quote type
JSON requires double quotes for both keys and string values. Single quotes are not valid JSON:
// Invalid
{'name': 'Alice', 'age': 30}
// Valid
{"name": "Alice", "age": 30}
Unmatched brackets
Every opening { needs a closing }. Every [ needs a ]. In nested structures, a missing closing bracket invalidates everything from the opening bracket to the end of the document:
// Invalid — missing closing } on the inner object
{"user": {"name": "Alice", "role": "admin"}
// Valid
{"user": {"name": "Alice", "role": "admin"}}
For deeply nested JSON, counting brackets by eye is unreliable. A formatter with bracket highlighting or a validator that shows the exact error position is much faster.
undefined and NaN
JavaScript developers frequently use undefined, NaN, and Infinity in code. These are not valid JSON values. Valid JSON number values are finite numbers only. Valid JSON has no undefined type:
// Invalid JSON (valid JavaScript)
{"result": undefined, "score": NaN}
// Valid JSON
{"result": null, "score": 0}
Comments in JSON
JSON does not support comments. // comment and /* comment */ are JavaScript syntax, not JSON. If you need to comment a JSON config file, either use a format that supports comments (YAML, TOML, JSON5) or store comments as a field value with a key like "_comment".
Frequently Asked Questions
How do I fix invalid JSON? Paste the JSON into a validator to find the exact line and character position of the error. The most common fixes are removing a trailing comma, replacing single quotes with double quotes, closing an unclosed brace or bracket, or removing a comment.
What does 'invalid JSON' mean in an API error? It means the request body could not be parsed as valid JSON. The server received the payload but the JSON parser rejected it before the application logic could run. Fix the syntax first — the API is not the problem.
What is the most common cause of invalid JSON? Trailing commas are the single most common cause. JSON does not allow a comma after the last item in an array or object. Developers who write JavaScript regularly make this mistake because modern JavaScript permits trailing commas and some linters even require them.
How do I find where JSON is broken? Use a JSON validator that shows line and column numbers for the error. Paste the payload in and the validator will point directly at the problem. If you do not have a validator handy, look at the last section you edited — that is where most errors appear.
Can I use JavaScript to validate JSON? Yes. JSON.parse() in a browser console or Node.js will throw a SyntaxError with a position if the JSON is invalid. It is not as user-friendly as a dedicated tool but works for quick checks without opening another tab.
Why does my JSON work in JavaScript but fail as an API payload? JavaScript is more permissive than JSON. JS allows trailing commas, single-quoted strings, unquoted object keys, and comments. None of these are valid JSON. Code that builds JSON via string concatenation or object literals can produce invalid JSON even when the JavaScript itself runs fine.
What should I do if an API returns a 400 error with no explanation? Validate the request body first. Paste the payload into a JSON validator before checking authentication, headers, or endpoint configuration. A malformed body is the most frequent cause of unexplained 400 errors and takes about ten seconds to rule out.
How do I prevent invalid JSON in the first place? Use JSON.stringify() in JavaScript instead of building JSON by hand with string concatenation. In other languages, use the built-in serialisation library. Only hand-write JSON in config files and always run a validator before committing or deploying.