fixjson.dev / guides

How to Fix Invalid JSON from LLMs

Why large language models produce broken JSON and how to repair trailing commas, fences, quotes, and truncated objects before parsing.

LLMs write text, not proven JSON

A language model predicts the next token. It does not maintain an open parse stack the way a JSON tokenizer would. That mismatch shows up as beautiful-looking but illegal documents: trailing commas after the last property, single-quoted strings, unquoted keys, Python booleans, markdown fences, commentary, and objects that stop mid-structure when the response length ends.

If you feed that text straight into JSON.parse, jq, or a strict API client, you get a hard failure even when the semantic intent is clear. The repair problem is therefore mechanical: normalize the text into RFC-compliant JSON before the consumer sees it.

Failure modes you will see most often

Trailing commas appear because many programming languages allow them and models train heavily on those corpora. Single quotes and unquoted keys appear when the model slips into JavaScript or Python object literals. True/False/None are classic Python leaks inside an otherwise JSON-shaped blob.

Markdown fences (```json … ```) show up when the model was rewarded for “helpful” formatting in chat. Smart/curly quotes sneak in when text was copied through a word processor. Truncation is different: the model ran out of budget, so braces never close. A fixer can often close trivial structure, but deeply incomplete payloads still need regeneration.

A practical repair workflow

First, strip chat wrappers and code fences. Second, normalize quotes and literals. Third, remove comments and trailing commas. Fourth, validate. Fifth—only if syntax is valid—apply schema checks so required fields and types match your contract.

fixjson.dev encodes that pipeline in the browser. Paste the model output, run Fix JSON, inspect which transforms applied, then copy pretty or minified output into your service. Keep original prompts and retry only when the structural intent is missing, not merely when punctuation is wrong.

Prevention tips for prompting

Ask for “strict JSON only, double quotes, no trailing commas, no markdown.” Prefer schemas or explicit field lists. For long documents, request chunked arrays you can concatenate, or use tool/function calling where the platform already validates JSON.

Even with better prompts, occasional invalid output remains normal. Treat repair as a permanent step in LLM→API pipelines rather than a one-off embarrassment.