JSON Repair API

Repair broken JSON: missing quotes and commas, trailing commas, comments, Python / JS literals, truncated text, code fences. Lists what was fixed.

GETPOSThttps://apibox.io.vn/v1/json-repair

Send text that should be JSON but does not parse, typically the answer of a language model, a JavaScript object literal, a Python dict or a log line, and get valid JSON back. The repair (jsonrepair library) quotes unquoted keys and strings, replaces single and typographic quotes, removes comments, trailing commas and ... placeholders, inserts missing commas and colons, converts True / False / None and undefined / NaN, unwraps JSONP and MongoDB calls (ObjectId("…")), joins "a" + "b", escapes raw line breaks and closes strings, brackets and braces of truncated output.

LLM answers are cut down to the JSON first: the first Markdown code block, or the text between the first { / [ and the last } / ]. Several top-level values (NDJSON, JSON lines) become an array, and JSON that was encoded as a JSON string is decoded. The response gives the repaired text (format keep, pretty or minify), the parsed data, the list of fixes with the position of their first occurrence, and, for inputs up to 20,000 characters, the exact character changes.

Pass a JSON Schema as schema to validate the repaired data in the same call (draft 2020-12). Valid JSON is returned unchanged with valid_input: true. Text that cannot be turned into JSON (for example only closing brackets) returns 422 with the position of the problem. Up to 5 MB; nothing is stored.

Parameters

NameInTypeRequiredDefault
jsonbody|querystringyesThe broken JSON text (or send it as a text/plain body).
formatbody|querystringnokeepkeep (layout of the input), pretty or minify.
indentbody|querynumberno2Indentation for format=pretty (0-8).
schemabodyobjectnoJSON Schema (draft 2020-12) to validate the repaired data.
parsebody|querybooleannotruefalse leaves out the parsed data.
unescapebody|querybooleannotrueDecode JSON that was sent as a JSON string ("{\"a\":1}").

Example

curl -X POST "https://apibox.io.vn/v1/json-repair" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"json":"```json\n{name: '\''Lan'\'', age: 30, tags: ['\''a'\'', '\''b'\'',], vip: True, note: None\n```"}'

Response

{
  "valid_input": false,
  "changed": true,
  "repaired": "{\"name\": \"Lan\", \"age\": 30, \"tags\": [\"a\", \"b\"], \"vip\": true, \"note\": null}",
  "fixes": [
    {
      "type": "markdown_fence",
      "message": "Removed Markdown code fences (```json … ```)",
      "position": 0,
      "count": 1
    },
    {
      "type": "unquoted_key",
      "message": "Quoted unquoted object keys",
      "position": 9,
      "count": 5
    },
    {
      "type": "single_quotes",
      "message": "Replaced single-quoted strings with double quotes",
      "position": 15,
      "count": 3
    },
    {
      "type": "trailing_comma",
      "message": "Removed trailing commas",
      "position": 46,
      "count": 1
    },
    {
      "type": "python_constant",
      "message": "Converted Python constants True / False / None to true / false / null",
      "position": 55,
      "count": 2
    },
    {
      "type": "unclosed_bracket",
      "message": "Closed brackets / braces left open (truncated JSON)",
      "position": 71,
      "count": 1
    }
  ],
  "changes": [
    {
      "position": 0,
      "removed": "```json\n",
      "added": ""
    },
    {
      "position": 9,
      "removed": "",
      "added": "\""
    },
    "…"
  ],
  "data": {
    "name": "Lan",
    "age": 30,
    "tags": [
      "a",
      "b"
    ],
    "vip": true,
    "note": null
  },
  "type": "object"
}

Use cases

  • Parse structured answers of ChatGPT, Claude or Gemini that are almost JSON
  • Clean JSON pasted from JavaScript code, Python prints or logs
  • Recover the beginning of a JSON response cut off by a timeout or token limit
  • Repair, then validate against a schema in one step in n8n / Make pipelines

FAQ

Does it change valid JSON?

No. Valid input is returned as is (valid_input: true), unless you ask for format=pretty or minify.

How do I know what was changed?

fixes names each kind of problem with its first position and count; for inputs up to 20,000 characters changes lists every inserted or removed character run.

Can it invent missing data?

No. Truncated values are closed where they stop (a cut string stays cut, open arrays and objects are closed); nothing is guessed.

Related APIs

Start with 1,000 free requests every month. Get free API key →