Skip to content
DevTinyTools

Errors

Why LLM JSON output breaks and how to fix it

An assistant writes JSON the way it writes prose — with a fence, a preamble and the occasional Python value. None of that is valid JSON, but all of it can be reversed without changing the data. Here is what the parser actually complains about, and the order a safe repair has to follow.

9 min readTool: JSON Repair

Prose habits meet a strict grammar

An assistant writes JSON the way a person writes it in a chat message: a fence around the block, a sentence before it, single quotes borrowed from Python, and a closing brace that sometimes never arrives. JSON is not a loose notation. RFC 8259 allows exactly one top-level value, double-quoted names, and nothing after the last member. Nearly every failure below is a habit from another language leaking through.

The useful part is that these faults are mechanical. None of them needs a model to fix, and none of them changes the data if the rewrite is done carefully. That is why a repair tool can list every edit it made instead of quietly handing back a cleaned string.

Six real failures, and what each one means

These messages came from parsing the input shown beside them with the local Node build on 18 November 2026. Chrome uses the same V8 engine, so you will usually see identical wording. The wording is not part of any specification — Firefox and Safari stop at the same character but phrase it differently.

Real parser messages for six common faults in assistant-generated JSON
InputV8 messageCause and fix
{"name": "Ada",}Expected double-quoted property name in JSON at position 15 (line 1 column 16)A trailing comma. The parser expected another key after the comma and reached the closing brace instead. Remove the comma before } or ] .
{name: "Ada"}Expected property name or '}' in JSON at position 1 (line 1 column 2)An unquoted key. JavaScript object literals allow it; JSON does not. Wrap the key in double quotes.
{"ok": True}Unexpected token 'T', "{"ok": True}" is not valid JSONA Python boolean. JSON spells it true, in lower case. Map True, False and None to true, false and null.
{"name": "Ada"Unexpected end of JSON inputThe object was never closed — the usual shape of a response cut off at a token limit. Close the open object, but treat the result as partial.
{"text": "line one line two"}Bad control character in string literal in JSON at position 18 (line 1 column 19)A literal newline inside a string. JSON requires it to be escaped as \n. Replace the raw control character with its escape sequence.
{"items": [1,2,]}Unexpected token ']', "{"items": [1,2,]}" is not valid JSONA trailing comma in an array, reported without a position by newer V8 builds. Remove the comma before ] .

Note the fourth row: a truncated object produces a message about the end of the input, not about a specific character. That is because the parser reached the end while still expecting a value or a closing brace.

Why repair order matters

A naive repair runs a list of substitutions over the whole string. That breaks on data: replacing True everywhere turns the harmless string{"note":"True story"} into {"note":"true story"}, silently changing what the document says. A repair that is safe has to know where strings are.

  • Extract the document first. A code fence and surrounding prose sit outside the JSON and can be removed without reading inside it.
  • Scan with string state. Track whether each character is inside a double-quoted string, a single-quoted string or neither. Structural fixes apply only outside strings; escapes and quote normalisation apply inside them.
  • Close what is actually open. Keep a stack of braces and brackets. A truncated document gets exactly the closers its stack is missing — no more.
  • Validate at the end. Run a strict JSON.parse on the result. If it still fails, show it as unrepaired rather than as a usable answer.

The last step is the one that makes the rest defensible. A repair tool that always returns something can never tell you when it failed. One that requires a strict parse can promise that whatever it shows will work downstream.

A worked example

Start with a response that collects five common faults in one block:

```json
{
  // parsed from a model
  name: 'Ada',
  active: True,
  skills: ['typescript', 'react',],
  score: NaN
}

After extraction, scanning and a strict parse, the document is:

{
  "name": "Ada",
  "active": true,
  "skills": ["typescript", "react"],
  "score": null
}

Each value is unchanged in meaning. The fence and comment are gone, two keys and two strings gained the quotes JSON requires, True became true, the trailing comma went away, and NaN — which JSON cannot express — became null. Run it yourself to see the per-change report.

What repair should not do

Some inputs look like the kind of thing a repair tool should fix, but changing them would change the data. It is better to refuse these than to guess.

  • NDJSON and JSONL. Several objects on separate lines are a stream, not one document. Joining them into an array invents a structure the source did not have.
  • Duplicate keys. When one object has the same name twice, a parser keeps one value and discards the other. A repair tool has no basis for choosing which one you meant.
  • Ambiguous numbers. 0x1F, .5 and +1 are invalid in JSON but have obvious decimal forms. They are still left alone, because a repair that guesses is a repair that can be wrong.
  • Missing fields. Closing a truncated object makes it parseable, not complete. The fields after the cut were never generated and cannot be recovered from the text.

Getting a model to emit clean JSON in the first place

Repair is a fallback. The better path is to ask for a single JSON value with no fence, no preamble and no commentary, and to keep the response short enough that it cannot be cut off. Where the provider supports a structured-output mode, use it: the model is constrained to a schema and cannot emit a trailing comma or an unquoted key.

Even then, validate the result. A constrained decoder guarantees the shape, not the truth of the values, and a business rule such as “end date after start date” still has to be checked by your own code.