You ask ChatGPT, Claude, or a local LLM for structured JSON — or copy a dictionary straight out of a Python traceback or JavaScript console — and your parser immediately throws SyntaxError: Unexpected token. Real-world broken JSON almost never fails for mysterious reasons; it fails because of a predictable handful of differences between JSON (RFC 8259) and Python, JavaScript, or streamed LLM outputs. This guide shows how to diagnose and repair every common type of malformed JSON automatically in your browser without uploading sensitive prompts or data.

Why LLM and Python outputs break strict JSON parsers

JSON looks almost identical to a JavaScript object literal or a Python dict, which tricks both humans and language models into mixing syntax. Strict JSON.parse() rejects all of these:

Broken snippetSourceWhy JSON.parse failsValid JSON fix
{'active': True, 'owner': None}Python print(d)Single quotes; capitalised True/False/None{"active": true, "owner": null}
{ name: "Asha", tags: ["a",], }JavaScript / TypeScriptUnquoted keys; trailing commas{"name": "Asha", "tags": ["a"]}
```json\n{"ok": true}\n```ChatGPT / ClaudeMarkdown code fence & prose wrapper{"ok": true}
{"items": [1, 2, 3LLM max_tokens cutoffTruncated output missing closing ]}{"items": [1, 2, 3]}
{"msg": "He said "hi""}LLM / log stringUnescaped double quotes inside string{"msg": "He said \"hi\""}
{"note": “smart quotes”}Docs / Slack / NotionCurly quotes (“ ” ‘ ’) instead of ASCII "{"note": "smart quotes"}
{"id": ObjectId("65a…"), "n": NaN}MongoDB / PythonBSON wrappers, NaN, Infinity, undefined{"id": "65a…", "n": null}
"{\"user\": \"Asha\"}"Double-encoded APIJSON stringified twice into an escaped string{"user": "Asha"}

Repairing broken JSON in one click

Instead of hunting down twenty single quotes and a missing bracket by hand, paste the broken payload into SnipFerry’s JSON Repair tool. It runs a dedicated lexer-based repair engine locally in your browser — never inside an eval() call — so it fixes structural syntax while leaving text inside string values intact, and lists every single fix it made so you can audit the output.

Worked example: Python dict + LLM truncation + comments

Consider this typical broken snippet pasted from a debugging session:

Here is the JSON output:
```json
// Generated config
{
  'service': 'billing',
  active: True,
  retries: None,
  "ports": [80, 443,],
  "quote": “ready”,
  "meta": {
    "region": "ap-south-1",
    "replicas": [1, 2

Pasting that directly into JSON Repair strips the prose and Markdown fence, removes the // comment, converts single and curly quotes to standard double quotes, turns True/None into true/null, removes the trailing comma, and closes the truncated array and objects:

{
  "service": "billing",
  "active": true,
  "retries": null,
  "ports": [80, 443],
  "quote": "ready",
  "meta": {
    "region": "ap-south-1",
    "replicas": [1, 2]
  }
}

How to stop LLMs from returning broken JSON in the first place

Repair is great for ad-hoc debugging and recovering truncated logs, but in production pipelines you want valid JSON straight from the model:

  1. Use native Structured Outputs / JSON Schema mode. Modern OpenAI, Anthropic, and Gemini APIs support constrained decoding against a JSON Schema, guaranteeing syntactically valid output that matches your exact keys and types. Generate a schema draft in seconds with JSON to JSON Schema (see What Is JSON Schema?).
  2. Watch max_tokens limits. The #1 cause of truncated JSON is hitting the response token ceiling mid-array. Check the API’s finish_reason ("length" / "max_tokens") before parsing, and paginate large outputs into smaller batches.
  3. Always validate with a schema library after parsing. Even valid JSON can have a string where you expected a number or omit a required key. Validate parsed payloads with Zod (JSON to Zod) or Pydantic (JSON to Python).

Converting Python dicts safely in code

When exporting a Python dictionary for JavaScript, Postman, or a web API, never copy the output of print(my_dict) (which uses Python’s repr() format with single quotes and True/None). Always serialise with Python’s standard json module:

import json
print(json.dumps(my_dict, indent=2, ensure_ascii=False))

If you already have a printed Python dict in a log file, paste it straight into JSON Repair — it converts single quotes, True, False, None, tuples (1, 2), and trailing commas without altering apostrophes inside double-quoted strings.

Double-encoded (stringified) JSON

Logs, webhooks, and message queues frequently wrap a JSON object inside another JSON string, producing a wall of backslashes like "{\"orderId\":42,\"status\":\"paid\"}". Both JSON Repair and JSON Unescape unwrap stringified JSON cleanly back into readable formatted JSON.

Always double-check repaired output

When JSON is severely broken — especially unescaped quotes inside prose or truncated keys — automated repair makes the best structural guess. Before pushing repaired data into a database or production config, run it through the JSON Validator to check for duplicate keys and precision warnings on 64-bit integers, and compare it against your expected structure with JSON Diff. For a complete reference of every RFC 8259 error message, read Common JSON Errors and How to Fix Them and How to Format and Validate JSON.