How to Fix Invalid JSON

Repair common JSON syntax errors and validate your data.

Invalid JSON usually comes from a small syntax problem: a trailing comma, a missing comma, a single quote, an unquoted key, a comment, or a mismatched bracket. Because JSON has strict rules, even one wrong character prevents the entire document from being parsed. This guide covers every common invalid JSON pattern, shows you exactly what the correct version looks like, and explains how to fix each error — either manually or using the JSON Formatter auto-fix feature.

Short answer: Invalid JSON is text that breaks the JSON syntax rules in RFC 8259, so a parser rejects it. The usual causes are a trailing comma, single quotes, an unquoted key, a comment, a missing comma, a raw line break inside a string, or a missing bracket. Paste your text above to see the exact line and column.

What does invalid JSON mean?

Invalid JSON means that text meant to be JSON does not follow the JSON grammar, so a parser cannot turn it into data. It is an error, not a warning. JSON.parse in JavaScript, json.loads in Python and the json type in PostgreSQL all stop at the first problem and report it instead of guessing what you meant.

Invalid JSON is not the same as valid JSON with the wrong content. A document that parses but lacks a required field is valid JSON; checking its shape takes a schema, for example with the JSON Schema Validator. The wording of the message depends on the parser, not on the mistake, so you will see "Unexpected token", "Expected property name or '}'" or "invalid input syntax for type json" for the same trailing comma. The line and column the parser reports are the useful part.

Why JSON Is So Strict About Syntax

JSON was designed as a lightweight data interchange format that any programming language could parse reliably. To achieve that universal reliability, the specification leaves no room for ambiguity. There are no optional semicolons, no shorthand for undefined values, no tolerance for trailing commas, and no support for comments. Every key must be a double-quoted string. Every value must be one of six types: string, number, boolean, null, object, or array. A single deviation causes parsers in JavaScript, Python, Java, Go, and every other language to throw an error and refuse to continue.

This strictness is intentional — it makes JSON a predictable, language-neutral format. But it also means that JSON written by hand, copied from documentation, generated by AI tools, or modified in a text editor without JSON awareness is prone to small mistakes that are hard to spot with the naked eye.

Invalid JSON syntax rules (RFC 8259)

JSON is defined by RFC 8259; ECMA-404 describes the same grammar. A text is valid when it follows these rules:

  • The top level can be any JSON value: an object, array, string, number, true, false or null.
  • Objects use { } with keys in double quotes, a colon after each key and commas between pairs. Arrays use [ ]. A comma after the last item is not allowed.
  • Strings use double quotes only. A double quote, a backslash and control characters (U+0000 to U+001F) must be escaped inside them.
  • Numbers are decimal, with no leading zeros, no leading plus sign, no hexadecimal, and no NaN or Infinity.
  • Only lowercase true, false and null are literals. Whitespace is space, tab, line feed and carriage return. There are no comments.

Text exchanged between systems must be UTF-8. If you want comments or trailing commas, JSON5 allows them, but it is a different format that needs its own parser, so convert it back to JSON before sending it to an API.

Complete List of Invalid JSON Patterns

1. Trailing comma after the last property

This is the single most common JSON error. Many developers come from JavaScript, where trailing commas in objects and arrays have been valid since ES5. JSON does not allow them.

Invalid:

{
  "name": "Ava",
  "role": "admin",
  "active": true,
}

Valid:

{
  "name": "Ava",
  "role": "admin",
  "active": true
}

The same rule applies to arrays. ["one", "two", "three",] is invalid. Remove the comma after the last element.

2. Single quotes instead of double quotes

JavaScript allows both single and double quotes for strings. JSON only allows double quotes — for both keys and string values.

Invalid:

{'name': 'Ava', 'role': 'admin'}

Valid:

{"name": "Ava", "role": "admin"}

Auto-fix handles this automatically. If you have a large payload with hundreds of single-quoted strings, paste it into the formatter and click Auto-fix rather than replacing them manually.

3. Unquoted object keys

In JavaScript object literals, keys do not need to be quoted. In JSON, every key must be a quoted string — no exceptions.

Invalid:

{name: "Ava", active: true, score: 42}

Valid:

{"name": "Ava", "active": true, "score": 42}

This error is common when copying JavaScript object literals from source code into a JSON file or API payload field.

4. Comments inside JSON

JSON has no comment syntax. Neither // single-line comments nor /* */ block comments are valid anywhere in a JSON document.

Invalid:

{
  // The user's display name
  "name": "Ava",
  "score": 42
}

Valid:

{
  "name": "Ava",
  "score": 42
}

If you need to annotate JSON for human readers, move comments to a separate documentation file or use a format like JSONC (JSON with Comments), which is supported by some editors but is not standard JSON.

5. Missing comma between properties

Each key-value pair in a JSON object, except the last, must be followed by a comma. A missing comma causes an "Unexpected token" error on the next property.

Invalid:

{
  "name": "Ava"
  "role": "admin"
}

Valid:

{
  "name": "Ava",
  "role": "admin"
}

6. Undefined, NaN, and Infinity values

JSON has no representation for undefined, NaN, or Infinity. These JavaScript-only values must be replaced before serialization.

Invalid:

{"result": NaN, "timeout": Infinity, "value": undefined}

Valid:

{"result": null, "timeout": 0, "value": null}

In JavaScript, JSON.stringify() silently drops undefined properties and converts NaN and Infinity to null. If your serializer is not JSON.stringify(), verify how it handles these edge cases.

7. Unescaped special characters in strings

Certain characters inside JSON strings must be escaped with a backslash. The most common that cause parse failures are double quotes and backslashes.

Invalid:

{"message": "He said "hello" to her", "path": "C:\Users\Ava"}

Valid:

{"message": "He said \"hello\" to her", "path": "C:\\Users\\Ava"}

Other characters requiring escaping: \n (newline), \t (tab), \r (carriage return). Newlines inside a string value must be written as \n, not as an actual line break.

8. Mismatched or missing brackets and braces

A JSON object must start with { and end with }. A JSON array must start with [ and end with ]. Missing one closing character — or nesting them incorrectly — causes the parser to fail, often at the very end of the document.

Invalid:

{
  "users": [
    {"name": "Ava"},
    {"name": "Ben"}
  ]

Valid:

{
  "users": [
    {"name": "Ava"},
    {"name": "Ben"}
  ]
}

Deeply nested JSON is especially prone to this. The tree view in the JSON Formatter shows the nesting structure visually, making it easy to spot where a closing bracket is missing.

9. Numeric values with leading zeros

JSON does not allow integers to have leading zeros, because some parsers interpret them as octal notation.

Invalid:

{"zip": 01234, "code": 007}

Valid:

{"zip": "01234", "code": "007"}

If the numeric identity matters — ZIP codes, padded reference codes — represent them as strings to preserve leading zeros.

10. Duplicate keys

The JSON specification says object names should be unique. Some parsers accept duplicates and use the last value; others use the first; others throw an error. Remove duplicates to ensure consistent behaviour across all parsers.

Ambiguous:

{"status": "ok", "status": "error"}

Safe:

{"status": "error"}

Have JSON that will not parse? Paste it into the box at the top of this page to see the exact line and column and have the formatter repair it for you, or open the formatter and press Auto Fix yourself.

Open the formatter

Invalid JSON examples and how to fix each

Each example below was run through JSON.parse in current Chrome. The message is Chrome's wording; Firefox and Safari word the same errors differently. The position counts characters from 0, while line and column start at 1. More cases are listed in Common JSON Errors.

Trailing comma

{"name": "Ava",}

Chrome: Expected double-quoted property name in JSON at position 15 (line 1 column 16). Remove the comma before }.

{"name": "Ava"}

Single quotes

{'name': 'Ava'}

Chrome: Expected property name or '}' in JSON at position 1 (line 1 column 2). Use double quotes for keys and strings.

{"name": "Ava"}

Missing comma

{"a": 1 "b": 2}

Chrome: Expected ',' or '}' after property value in JSON at position 8 (line 1 column 9). Add the comma after the first value.

{"a": 1, "b": 2}

Line break inside a string

{"note": "line one
line two"}

Chrome: Bad control character in string literal in JSON at position 18 (line 1 column 19). Write the break as \n.

{"note": "line one\nline two"}

Invalid characters in JSON

Some invalid JSON looks correct on screen because the problem is a character that is hard to see. Curly quotes from word processors and chat apps are not the straight double quote JSON requires. A byte order mark (U+FEFF), a non-breaking space (U+00A0) or a zero-width space (U+200B) is not JSON whitespace, which is limited to space, tab, line feed and carriage return. A real tab or line break inside a string is a control character and must be escaped.

{“name”: “Ava”}

Chrome: Expected property name or '}' in JSON at position 1 (line 1 column 2). Replace the curly quotes with straight ones.

Backslashes need care too. In "C:\Users\Ava" written with single backslashes, \U is not a valid escape, so write each backslash twice.

{"path": "C:\Users\Ava"}

Chrome: Bad escaped character in JSON at position 13 (line 1 column 14).

{"path": "C:\\Users\\Ava"}

Invalid JSON primitive and invalid input syntax for type json

These two messages come from servers rather than browsers, but the cause is the same: the text handed to the JSON parser is not JSON.

PostgreSQL raises ERROR: invalid input syntax for type json when a json or jsonb column or cast receives bad text. The DETAIL line names the problem, for example Token "'" is invalid. for single quotes, Expected string, but found "}". for a trailing comma, or The input string ended unexpectedly. for truncated JSON. Pass the value as a bound parameter produced by a JSON library instead of building the string by hand.

Invalid JSON primitive is an ASP.NET message from JavaScriptSerializer. It usually means the request body was not JSON at all, for instance form-encoded text such as name=Ava or the word undefined. Send a real JSON body with the Content-Type: application/json header. System.Text.Json words the same problem as 'x' is an invalid start of a value. For the browser version of these errors, see JSON Parse Error: Unexpected Token.

Invalid JSON payload or response from an API

When an API call reports invalid JSON, look at what was actually sent or received before you edit anything. Responses are often not JSON: a gateway error or login redirect returns an HTML page, and parsing it fails at the first character.

<!DOCTYPE html><html><body>Bad Gateway</body></html>

Chrome: Unexpected token '<', "<!DOCTYPE "... is not valid JSON. Check the status code and the Content-Type header before parsing.

An empty body fails with Unexpected end of JSON input, and a body cut off in transit fails where the text ends.

{"users": [{"id": 1}

Chrome: Expected ',' or ']' after array element in JSON at position 20 (line 1 column 21).

For requests, build the body with JSON.stringify or your language's JSON library, not by joining strings, so quotes and line breaks are escaped for you.

Step-by-Step: Fix Invalid JSON with JSON Formatter Hub

  1. Paste your JSON into the JSON Formatter editor on the left.
  2. Click Format. If the JSON is invalid, the editor highlights the first error with a line and column number.
  3. Click Auto-fix. This repairs trailing commas, missing commas between members on separate lines, single-quoted strings, unquoted keys, comments, Python-style True, False and None, raw line breaks in strings and Markdown code fences automatically.
  4. Click Format again to confirm the result is now valid. The editor applies indentation without showing any error marker.
  5. Use Remove Whitespace to minify, or Export/Download to save the clean output as JSON, CSV, YAML, or XML.

Once your JSON is valid, generating a schema from it with the JSON Schema Generator lets you catch the same structural errors automatically the next time the data changes.

How to fix invalid JSON automatically

Paste the text into the box at the top and press Fix it in the formatter (Auto Fix). The formatter opens with your text loaded and runs Auto Fix once, so you land on the repaired JSON and a one-line summary such as "Removed 2 trailing commas, converted 3 single-quoted strings". The Undo button in the green message restores what you pasted.

Auto Fix works on the text, not on parsed data. It removes comments only outside strings, leaves apostrophes and URLs inside strings alone, keeps every number as typed, including 12345678901234567890 and 1.0, and returns valid JSON unchanged. It also converts single-quoted strings, quotes bare keys, escapes raw line breaks and tabs in strings, replaces True, False and None, strips Markdown fences and text around the JSON, and adds a missing comma only when a new quoted key starts on the next line. It cannot complete truncated JSON or guess missing brackets or values; then it changes nothing and reports the first problem. For model output, see fixing AI-generated JSON.

When Auto-fix Cannot Repair the JSON

Auto-fix handles the most common mechanical errors, but there are situations where no tool can safely repair JSON without human input. If the JSON was truncated — for example, copied from a log that cut off mid-payload — the tool can validate what it has but cannot guess missing values or closing structures. If the document has deeply inconsistent nesting, such as an array closed with a brace instead of a bracket, fix that manually using the line number the validator provides.

In these cases, work from the first reported error. Fix one problem at a time and revalidate after each change. Parsers typically report only the first error found, so fixing the first one often reveals additional errors that were hidden earlier.

Frequently Asked Questions

Why does my JSON look correct but still fail to parse?

The most common hidden issue is a non-visible character: a Unicode zero-width space, a byte-order mark (BOM) at the start of the file, or a curly typographic quote (“ instead of ") copied from a word processor or documentation site. Paste the text into the formatter — if it shows a character error at position 0 or 1, a BOM or invisible character is the likely cause.

Can I add comments to JSON?

Not in standard JSON. If you need annotated JSON for configuration files, consider JSONC (used by VS Code settings) or JSON5, both of which support comments. For API payloads and data exchange, comments are not appropriate — use API documentation or a JSON Schema alongside the data instead.

Why does JavaScript allow things that JSON does not?

JavaScript object literal syntax is more permissive than JSON. JSON was derived from a strict subset of JavaScript, not the full language. As JavaScript evolved, its object syntax gained features — trailing commas, computed keys, shorthand properties — that were never added to the JSON specification. This divergence is the source of most JSON errors made by JavaScript developers.

Is there a maximum nesting depth for JSON?

The JSON specification does not define a maximum depth, but individual parsers impose their own limits. Most production parsers handle hundreds of nesting levels safely. If you are generating deeply nested JSON programmatically, it is worth reconsidering the data structure — most real-world JSON is fewer than ten levels deep, and flatter structures are easier to work with across all languages.

What is the difference between invalid JSON and malformed JSON?

The terms are used interchangeably in practice. Strictly speaking, both refer to text that does not conform to the JSON specification and cannot be parsed by a standard JSON parser. Some developers use "malformed" specifically for structurally broken documents (unclosed brackets, truncated data) and "invalid" for syntactically wrong but complete documents (trailing commas, unquoted keys), but there is no universal distinction.

What does "Invalid JSON primitive" mean?

It is an ASP.NET error from JavaScriptSerializer. It usually means the request body was not JSON, for example form-encoded text such as name=Ava or the word undefined. Send a JSON body with the Content-Type: application/json header, and check the exact text you send in the box at the top of this page.

Try what you just learned — paste your JSON into the formatter and validate it instantly. Free, browser-only, no data sent anywhere.

Related Guides

Need to repair a payload? Paste invalid JSON and use Format or Auto-fix.

Fix JSON Now