JSON Repair API
Repair broken JSON: missing quotes and commas, trailing commas, comments, Python / JS literals, truncated text, code fences. Lists what was fixed.
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
| Name | In | Type | Required | Default | |
|---|---|---|---|---|---|
json | body|query | string | yes | The broken JSON text (or send it as a text/plain body). | |
format | body|query | string | no | keep | keep (layout of the input), pretty or minify. |
indent | body|query | number | no | 2 | Indentation for format=pretty (0-8). |
schema | body | object | no | JSON Schema (draft 2020-12) to validate the repaired data. | |
parse | body|query | boolean | no | true | false leaves out the parsed data. |
unescape | body|query | boolean | no | true | Decode 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```"}'const res = await fetch('https://apibox.io.vn/v1/json-repair', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"json": "```json\n{name: 'Lan', age: 30, tags: ['a', 'b',], vip: True, note: None\n```"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/json-repair",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"json": "```json\n{name: 'Lan', age: 30, tags: ['a', 'b',], vip: True, note: None\n```"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/json-repair');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"json":"```json\\n{name: \'Lan\', age: 30, tags: [\'a\', \'b\',], vip: True, note: None\\n```"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));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
JSON Validator
Check JSON syntax with exact line and column of errors, and validate data against a JSON Schema (draft 4, 7, 2019-09 or 2020-12).
{ }JSON Formatter
Pretty print JSON with 0-8 spaces or tabs, minify it, or sort object keys recursively. Syntax errors are reported with line and column.
CSVCSV to JSON
Convert CSV into an array of JSON objects using the header row, with delimiter auto-detection, number and boolean typing, and row-level errors.