Back to Blog

    JsonifyTools field guide

    Fix LLM JSON Output Without Hiding Schema Errors

    A careful workflow for parsing, repairing, and validating model-generated JSON without masking missing fields or invented values.

    By JsonifyTools4 min read
    GUARDED PARSING: parse → repair → validate — keep syntax separate from shapeRAWcapturePARSErepair onceSCHEMAvalidateGUARDED PARSINGparse → repair → validatekeep syntax separate from shape
    A diagram illustrating keep syntax separate from shape.

    A model response can look like JSON while still failing JSON.parse: a trailing comma, a Markdown fence, a smart quote, or explanatory text before the object is enough. In an automated pipeline, the tempting move is to repair whatever arrives. That can get a batch moving, but it can also hide a missing field or turn an ambiguous answer into data your application trusts.

    Check common model-output errors

    Separate extraction, syntax, and schema failures

    Start by capturing the raw response. If the model wrapped a JSON object in a code fence, extraction is a separate step from repairing JSON syntax. If the response contains prose mixed with the object, a repairer should not be expected to guess where the intended document begins and ends. Make the expected output format explicit at generation time and keep extraction rules narrow.

    Repairable-looking syntax

    Repairable-looking syntax
    { "id": "task-7", "done": true, }

    Not enough information

    Not enough information
    The task is done.
    { "id": "task-7" }
    Use this summary in your report.

    The left example has one common syntax defect. The right has multiple plausible pieces of output; extracting a particular object is a product rule, not a syntax correction. Preserve raw output for debugging and record which step rejected it.

    Use a pipeline that makes each decision visible

    1. 1Capture the raw model response
    2. 2Extract only the expected JSON region
    3. 3Parse; repair recognized errors once
    4. 4Validate shape and business rules
    Guarded JavaScript parsing
    let value;
    try {
      value = JSON.parse(candidate);
    } catch (error) {
      // Keep candidate for diagnosis; do not silently accept a guess.
      throw new Error("Model returned invalid JSON", { cause: error });
    }

    A repair step can sit in the catch path when the repair behavior is understood and allowed. Parse the repaired result again; a repair function returning text does not prove that the result is valid. Avoid an unbounded repair-and-retry loop. One repair attempt followed by a controlled retry or rejection is easier to audit.

    Know when to repair and when to retry

    Repair may be reasonable for a small, recognizable punctuation issue in a noncritical task. Retry or reject when the answer is truncated, the model returned prose outside the expected boundary, required content is missing, or the intended value is unclear. A syntactically valid answer can still be semantically wrong, so downstream validation remains essential.

    Validate the expected shape

    After parsing, check required properties, allowed enum values, and types. For example, { "priority": "high" } parses, but a consumer expecting a numeric priority should reject it. JSON Schema can express much of that shape validation; application rules such as whether an identifier exists in your database still belong to the application.

    The JSON Fixer can help inspect common malformed syntax. Follow with the JSON Schema Validator for structural requirements, and use the JSON Viewer to inspect the parsed result. For a catalogue of punctuation and quoting errors, see how to fix JSON syntax errors.

    Constrain the output before it reaches the parser

    Ask for one object with a defined shape and no surrounding commentary, but treat prompting as guidance rather than a guarantee. If the model or API supports a structured-output or schema-constrained mode, use the provider’s documented contract and still validate the received value. Keep the extraction rule narrow so a parser does not accidentally accept a JSON-looking example quoted inside an explanation.

    Keep retries bounded and diagnosable

    Record whether a failure happened during extraction, parsing, repair, or schema validation. A retry should include a concise error category or path, not a dump of sensitive application data. Cap attempts and route repeated failures to a visible error path; silently substituting an empty object can turn a format problem into a much harder downstream defect.

    Tags

    • fix invalid JSON from LLM
    • LLM JSON parse error
    • AI structured output
    • repair malformed model output
    • JSON Schema validation
    • LLM response parsing
    • trailing comma JSON
    • model output retry