How to Find and Fix Invalid JSON With Real Error Examples
Fix invalid JSON with real examples: trailing commas, missing quotes, bad escaping, comments, and mismatched brackets.
Quick answer
JSON must use double-quoted keys and strings, no trailing commas, and no comments. Paste your text into the JSON Formatter, read the parse error location, fix that issue, and validate again until the payload parses.
Key takeaways
- ›Trailing commas and unquoted keys are the most common JSON syntax errors in API payloads.
- ›JSON is not JavaScript — undefined, comments, and single-quoted strings are invalid.
- ›Fix the first reported parse error, then re-validate; one typo often causes multiple error messages.
- ›ByteToolBox validates JSON locally in your browser without uploading your data.
Apply this guide with the JSON Formatter
Open JSON FormatterInvalid JSON breaks builds, API clients, and config deploys. The fastest workflow is: paste into the JSON Formatter, read the line/column error, fix the syntax issue, and validate again — all locally in your browser.
What makes JSON invalid?
JSON (RFC 8259) is stricter than JavaScript object literals. Common violations:
| Problem | Invalid example | Why it fails |
|---|---|---|
| Trailing comma | {"a": 1,} | No trailing commas after last property |
| Unquoted keys | {name: "x"} | Keys must be double-quoted strings |
| Single quotes | {'a': 1} | Strings must use " |
| Comments | // note\n{"a":1} | JSON has no comment syntax |
| Trailing decimal | {"n": 1.} | Malformed number |
| Mismatched brackets | {"a": [1,2}} | [ closed with } |
How to use ByteToolBox JSON Formatter to find errors
- Open the JSON Formatter
- Paste your payload into the editor
- Click Validate (or format) — the tool reports parse errors with position hints
- Fix one issue at a time; a single missing quote often causes cascading errors
- Format once valid to confirm structure and indentation
ByteToolBox processes JSON in your browser — your payload is not uploaded to a server for formatting.
Example 1: trailing commas
Invalid:
{
"users": [
{"id": 1, "name": "Ada"},
{"id": 2, "name": "Lin"},
]
}Fixed:
{
"users": [
{"id": 1, "name": "Ada"},
{"id": 2, "name": "Lin"}
]
}Remove the comma after the last array element and the last object property.
Example 2: missing quotes around keys
Invalid:
{
userId: 42,
active: true
}Fixed:
{
"userId": 42,
"active": true
}Example 3: bad string escaping
Invalid:
{
"path": "C:\Users\dev\config.json"
}In JSON, backslashes must be escaped: \\.
Fixed:
{
"path": "C:\\Users\\dev\\config.json"
}Invalid newline in string:
{
"message": "line one
line two"
}Fixed (use \n or split fields):
{
"message": "line one\nline two"
}Example 4: comments inside JSON
Invalid (JSONC / editor-style):
{
// production API URL
"baseUrl": "https://api.example.com"
}JSON parsers reject // and /* */. Strip comments or use a JSONC-aware tool for config files, then export strict JSON for APIs.
Fixed:
{
"baseUrl": "https://api.example.com"
}Example 5: mismatched brackets
Invalid:
{
"items": [
{"sku": "A1", "qty": 3}
}
}The items array opened with [ but closed with }.
Fixed:
{
"items": [
{"sku": "A1", "qty": 3}
]
}Tip: In the formatter, collapse brackets mentally or use format-after-fix to visualize nesting.
Format vs validate vs minify
| Action | Purpose |
|---|---|
| Validate | Confirm parseable JSON; surface first syntax error |
| Format / beautify | Add indentation for readability after valid parse |
| Minify | Remove whitespace for smaller wire size |
Always validate before minify. Minifying invalid text does not produce valid JSON.
Common JSON debugging mistakes
- Assuming JSON.parse errors mean bad data — sometimes it's a BOM or smart quotes from copy/paste
- Using eval or JS object syntax —
undefined, functions, andNaNare not JSON - Fixing multiple errors at once — fix the first reported error, re-validate
- Ignoring encoding — UTF-8 with BOM can break the first token
- Shipping config with comments — runtime JSON.parse will fail in production
Best practices for API payloads
- Validate JSON in CI with
JSON.parseor schema tools - Use JSON Schema for contracts
- Reject unknown fields explicitly in strict APIs
- Log the raw body (redacted) when parse fails — not just "invalid JSON"
- Return
400with a clear error message and optionalpositionif your parser provides it
See also JSON Best Practices for API Development and Optimizing JSON Performance.