AI JSON guide

AI JSON Format

Clean, format and validate JSON for AI workflows, structured prompts and model responses.

Open AI JSON Repair

How to do it

  1. Repair first: send any AI response into AI JSON Repair to remove fences, prose and JS-style syntax.
  2. Format second: feed the repaired text into JSON Formatter for readable indentation.
  3. Validate third: run JSON Validator before parsing the output in your service.
  4. Type fourth (optional): use JSON to TypeScript or JSON to Schema to lock the contract once the data is clean.

Three stages of an AI JSON formatting pipeline

Treat AI JSON formatting as a pipeline, not a one-click action. The repair stage is permissive and tolerates broken input. The format stage assumes valid JSON and enforces consistent indentation. The validate stage rejects anything that would crash JSON.parse in your service. Splitting the work this way gives you clearer error messages when something goes wrong, because each stage only owns one type of failure.

Keeping the stages separate also makes it easy to swap any single piece. If you switch from Doubao to a different model, only the repair stage needs to learn new quirks. The formatter and validator stay the same.

When to use AI fallback vs local repair

Local repair handles the long tail of LLM output mistakes deterministically. It is fast, free and never sends your data anywhere. Reach for the AI fallback when the structure is so broken that no rule-based pass can recover the original intent — for example, when the model returned a paragraph of natural language that needs to be reshaped into a JSON object based on its meaning.

  • Use local repair for: code fences, single quotes, trailing commas, comments, smart quotes, leading prose.
  • Use AI fallback for: deeply nested broken arrays, model output that mixes prose and JSON, messy text-to-JSON conversions.
  • Skip the AI fallback entirely if the input is already valid JSON or only needs whitespace cleanup.

Integrating the workflow into a service

If you build on top of structured outputs from Doubao or OpenAI, run repair as a defensive middleware before JSON.parse. Surface the repair logs so you can spot drifting patterns in model output, then feed those patterns back into your prompt or schema. This site mirrors the same pipeline so you can sanity-check responses by hand without writing throwaway code.

Examples

Tool-call payload with prose wrapper

Input

Sure! Here is the function call:
        
        { name: 'create_invoice', arguments: { 'amount': 99.5, 'currency': 'USD' }, }

Output

{
          "name": "create_invoice",
          "arguments": {
            "amount": 99.5,
            "currency": "USD"
          }
        }

Repair removes the prose, quotes the unquoted key, normalizes single quotes and drops the trailing comma.

Recommended tool

Related guides

FAQ

Why does AI JSON need formatting?

AI output may include explanations, markdown fences or JSON-like syntax that strict parsers reject.

Can I use this for structured outputs?

Yes. It is useful for verifying and cleaning structured outputs before they hit your downstream code.

Should I validate after formatting?

Yes. Validation catches subtle issues like accidentally string-quoted numbers, unmatched brackets or duplicate keys.

Does this replace prompt engineering?

No. Strict prompts and JSON Schema constraints are still the cheapest fix. The pipeline is a safety net, not a substitute.

Is the result safe to send to JSON.parse?

After validate passes, yes. The pipeline is designed to produce parser-ready output.