Structured JSON Repair
structured_json_repairRepair messy or invalid JSON (the kind LLMs and tools often emit) into clean, valid JSON, and optionally validate/coerce it against a JSON Schema. Pure deterministic compute — no network or model calls.
What it fixes: trailing commas, single-quoted strings, unquoted keys, Python literals (None/True/False), NaN/Infinity, Markdown code-fence wrappers, and truncated/garbled tails.
When to use: you received text that should be JSON but JSON.parse fails, or you have JSON that must conform to a specific schema and want types coerced (e.g. "36" -> 36, "true" -> true).
When NOT to use: the input is already known-valid JSON and no schema check is needed.
Args:
input (string, required): the raw/malformed JSON text.
schema (object, optional): a JSON Schema (draft 2020-12) to validate and coerce against.
coerce (boolean, optional, default true): coerce primitive types to satisfy the schema before validating.
Returns structuredContent: { "ok": boolean, // true if valid JSON (and schema-valid when a schema was given) "data": any, // the repaired/validated JSON value; null if unfixable "changed": boolean, // true if any repair or coercion modified the input "errors": string[], // actionable messages when ok is false "repairs": string[] // description of each fix applied }
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | Raw or malformed JSON text to repair. Examples: "{name: 'Ada', age: '36',}", a ```json fenced block, or a truncated '{"items":[1,2,3'. | |
| coerce | No | When true (default), coerce primitives to satisfy the schema before validating (e.g. "36" -> 36). | |
| schema | No | Optional JSON Schema (draft 2020-12) object to validate and coerce the repaired JSON against. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ok | Yes | True if the result is valid JSON (and schema-valid when a schema was provided). | |
| data | No | The repaired/validated JSON value (object, array, or primitive). null when repair failed. | |
| errors | Yes | Actionable error messages when ok is false (empty when ok is true). | |
| changed | Yes | True if any repair or coercion changed the input. | |
| repairs | Yes | Human-readable description of each repair or coercion applied. |