# Why LLM JSON output fails to parse, and how to find the error

Published 2026-11-07. Prices as of 2026-11-22.

An LLM returns invalid JSON because it predicts text, and plain generation does not enforce the JSON grammar. The usual breakages are a code fence or a sentence around the object, a trailing comma, single quotes, a comment, or a reply cut off at the output limit. A strict validator shows the first error and where it sits.

## What a strict parser rejects

JSON is defined by RFC 8259, and the parsers in browsers and in most languages follow it closely. A model that writes something JSON-like but outside that grammar makes the parse fail, and the error message rarely says which habit caused it. These are the ones that show up most in model replies.

- Text around the object. A reply that opens with "Here is the JSON:" or wraps the object in a markdown code fence is not a JSON document, so the parser fails at the first character.
- A trailing comma. A comma after the last item of an array or object is accepted in JavaScript source and in many config formats. In JSON a comma only goes between two values.
- Single quotes. Keys and strings use double quotes. A reply that follows Python dict style with single quotes fails at the first key.
- Comments. JSON has no comment syntax, so a line such as // optional or a /* note */ block is an error.
- Python literals. True, False and None are not JSON. The literal names are true, false and null, and they must be lowercase.
- Odd numbers. A leading zero as in 007, a leading plus sign, NaN and Infinity are all outside the number grammar.
- Raw control characters in a string. A line break inside a string value has to be written as \n. A real line break there is invalid.

## A reply cut off at the output limit

When a response stops because it reached the maximum output length, the JSON ends in the middle of a value. The parser reports an unexpected end, and the text looks fine up to that point, which makes this failure easy to misread as a formatting bug.

Most APIs return a finish or stop reason with every response. A value such as length or max_tokens means the reply was cut off. Read it before you parse, and raise the output limit if the payload is simply larger than you allowed for.

## Finding the first error

A long payload is slow to read by eye, and each browser words its parse errors differently. A validator that reports the line, the column and a reason for the first failure turns the search into a lookup. [Grabivo's JSON formatter and validator](https://grabivo.app/json-formatter/) runs in the browser tab, uploads nothing, follows RFC 8259 and accepts up to 5 MB of input.

1. Paste the raw text of the model reply, not an object your code has already half-parsed or repaired.
2. Read the reason and the position of the first error. Fix that one and check again, because later errors are often hidden behind it.
3. If the same mistake keeps coming back, change the request instead of patching the output: ask for raw JSON only, show the exact shape you expect, or switch to a schema-constrained mode.

## Preventing the failure

- Use schema-constrained output where the provider has it. OpenAI documents Structured Outputs as making the model produce responses that adhere to a supplied JSON Schema. Support differs by provider and by model, so read the docs for the model you actually call.
- Function calling is the other common route to structured arguments. The [cheapest tool-calling models](https://sovyron.com/cheapest/tool-calling-models/) page lists the models that support it, ranked by price.
- Validate every response and keep retries rare. A retry on a metered API bills the whole prompt again plus a new reply, so a parse failure costs more as the prompt grows.
- Send minified JSON in prompts. Indentation adds characters that a tokenizer may turn into extra tokens. How many depends on the tokenizer, so count a real payload with your provider's token counter before you rely on a saving.

## FAQ

### Why does the model wrap its JSON in a markdown code block?

Many chat models format code in a fenced block by default. The fence is not part of the JSON document, so strip it before parsing, ask for raw JSON only, or use a schema-constrained mode that returns the object without wrapping it.

### Is a trailing comma valid JSON?

No. RFC 8259 allows a comma only between two values, so a comma after the last element of an array or object makes the document invalid, even though JavaScript source accepts it.

### How can I tell that a reply was cut off?

Check the finish or stop reason in the API response. A value such as length or max_tokens means the model hit the output limit. The JSON then ends in the middle of a value and the parser reports an unexpected end.

### Is it safe to paste API output into an online JSON validator?

It depends on the tool. Some send the text to a server. Grabivo states that its validator runs in the browser tab and uploads nothing. Still, keep keys, personal data and production payloads out of any page you have not checked.

## Sources

- [RFC 8259: The JSON Data Interchange Format](https://www.rfc-editor.org/rfc/rfc8259)
- [OpenAI Structured Outputs guide](https://developers.openai.com/api/docs/guides/structured-outputs)

HTML page: https://sovyron.com/articles/llm-json-output-errors/
Methodology: https://sovyron.com/methodology/#blend
