apyhub
Back
▣ INGESTION KIT · MODEL KIT

Repair Broken JSON API

What it does

The Repair Broken JSON API turns broken JSON into valid JSON. Send text from an AI model or any other source, with or without prose around it, and the API returns the parsed data, a strict JSON string, and a list of every fix it made.

Put the text in json (up to 1,000,000 characters). The API fixes trailing commas, single quotes, unquoted keys, // comments, Python True and False, markdown code fences and input that ends early. If the text holds several JSON blocks, extract picks first (the default), largest or all. Add a JSON Schema in schema to check the repaired data. Set coerce to true to cast obvious mismatches, such as the string "12" to the number 12. Each cast is listed in fixes.

Teams use the Repair Broken JSON API to clean up model answers before parsing them, to check structured output against a schema, and to log what changed in each response.

It repairs syntax only. Read fixes and schemaValid before you trust repaired data.

POST
JSON Repair API - fix broken LLM JSON, validate schema
https://api.eu.apyhub.com/martin10291/json-repair-validate/json-repair-validate

QUICKSTART

GUIDE

Quickstart

Send a model answer that has prose, a code fence and broken JSON, plus a schema to check it against.

curl -X POST "https://api.eu.apyhub.com/martin10291/json-repair-validate/json-repair-validate" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "json": "Sure! Here it is:\n```json\n{name: \"Ada\", tags: [\"math\", \"code\",], active: True, // note\n \"born\": \"1815\"}\n```",
    "schema": {"type": "object", "required": ["name", "born"], "properties": {"name": {"type": "string"}, "born": {"type": "integer"}, "tags": {"type": "array", "items": {"type": "string"}}}},
    "coerce": true
  }'

What you'll get back

Returns a JSON object with count and items. Each item holds the repaired data, the compact json text, the list of fixes, and the schema result in schemaValid and schemaErrors.

{
  "count": 1,
  "items": [
    {
      "data": {"active": true, "born": 1815, "name": "Ada", "tags": ["math", "code"]},
      "fixCount": 9,
      "fixes": [
        {"at": 18, "detail": "markdown code fence removed", "type": "code_fence"},
        {"at": 0, "detail": "17 characters of text around the JSON ignored", "type": "prose_removed"},
        {"at": 27, "detail": "key quoted", "type": "unquoted_key"},
        {"at": 61, "detail": "trailing comma removed", "type": "trailing_comma"},
        {"at": 73, "detail": "True -> true", "type": "python_literal"},
        {"at": null, "detail": "/born: \"1815\" -> integer 1815", "type": "coerced"}
      ],
      "inputChars": 107,
      "json": "{\"name\":\"Ada\",\"tags\":[\"math\",\"code\"],\"active\":true,\"born\":1815}",
      "ms": 5.4,
      "repaired": true,
      "schemaErrors": [],
      "schemaValid": true,
      "truncated": false,
      "valid": false
    }
  ]
}

The fixes list is cut to 6 of its 9 entries here.

TRY ITLIVE · 100 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.
body*
The broken JSON text, or a whole model answer with prose around it. Max 1,000,000 characters.
When true and a schema is sent, casts obvious type mismatches ("12" to 12, "true" to true, a single value to [value]) and lists each cast in fixes.
schema
A JSON Schema to check the repaired data against. Remote $ref values are not fetched. The format keyword is not enforced.
Which JSON block to use when the text holds several. With all, data is an array of every block.

About this endpoint

What it does

Repairs broken JSON text, takes the JSON out of surrounding prose when needed, and returns the parsed result with every fix it applied. If you send a JSON Schema, it also checks the repaired data against that schema.

Request Body

ParameterTypeDescription
jsonStringRequired. The broken JSON text, or a whole model answer with prose around it. Max 1,000,000 characters.
coerceBooleanWhen true and a schema is sent, casts obvious type mismatches ("12" to 12, "true" to true, a single value to [value]) and lists each cast in fixes. Default: false.
schemaObjectA JSON Schema to check the repaired data against. Remote $ref values are not fetched. The format keyword (for example email) is not enforced.
extractENUMWhich JSON block to use when the text holds several. Allowed values: first, largest, all. Default: first. With all, data is an array of every block.

Response

Returns a JSON object with count and items. count is the number of rows in items. Each request returns one row.

ParameterTypeDescription
countIntegerNumber of rows in items.
items[].dataAnyThe parsed JSON value. Can be an object, array, string, number, boolean or null. null when parsing failed.
items[].jsonStringThe same value as compact, strictly valid RFC 8259 JSON text. null when parsing failed.
items[].validBooleantrue only when the input was already valid JSON and needed no repairs.
items[].repairedBooleantrue when broken input was repaired, or coerced to match the schema. false when the input was already valid or could not be parsed.
items[].fixCountIntegerNumber of repairs and casts applied.
items[].fixesObject ArrayOne entry per repair. Empty when nothing was changed.
items[].fixes[].typeStringKind of repair: code_fence, prose_removed, single_quotes, unquoted_key, trailing_comma, python_literal, comment, truncated_closed or coerced.
items[].fixes[].detailStringPlain description of the change.
items[].fixes[].atIntegerCharacter offset in the input. null for coerced entries.
items[].truncatedBooleantrue when the input ended early and open strings, arrays or objects were closed for you.
items[].inputCharsIntegerLength of the input text in characters.
items[].schemaValidBooleantrue when the data matches the schema, false when it does not. null when no schema was sent.
items[].schemaErrorsObject ArraySchema problems, each with path, message and validator. Empty when the data is valid or no schema was sent.
items[].errorStringPresent only when no JSON value could be found.
items[].errorAtIntegerCharacter offset of the failure. Present only with error.
items[].msNumberProcessing time in milliseconds.
items[].cachedBooleanPresent and true when the row was served from a cache.
items[].cacheAgeSecondsIntegerAge of the cached row in seconds. Present only when cached is true.

Errors

A request the API cannot accept returns status 400 with a JSON body that has code (invalid_input), error and message. This happens when json is missing, when extract is not one of the allowed values, and when the text contains hidden or control characters such as zero width joiners.

Text that holds no JSON at all returns status 200 with data set to null and an error message in the row.

Notes

  • Check fixes before you rely on the result. Repairs are heuristic.
  • When a key is missing its colon or closing quote, the repair can merge or drop values without an error. Compare data with your source for such input.
  • Numbers with very long decimals can be shortened without an entry in fixes.
  • With coerce set to true, a string such as "0012" becomes the number 12.
▣ COMMON ERRORS

Errors any endpoint can return

400bad_request

Required parameter missing or malformed body.

401unauthorized

API key missing, revoked, or not authorized for this service.

429rate_limited

Your plan's per-second rate exceeded. Retry with exponential backoff.

503upstream_busy

Backend temporarily unavailable. Try again in a few seconds.