← Alle Playbooks
Playbook· build

Reliable JSON from any AI, so your automation doesn't break

Why AI answers crash your automation and how to force a JSON format in ten steps that parses every time. Provider-independent, with ready-made prompt building blocks.

The moment you put an AI into an automation, you no longer need a nice free-text result but data a machine can process further. Usually that's JSON. And that's exactly where the problems start. The AI delivers clean JSON 90 percent of the time and on the tenth try it puts a nice introductory sentence in front, adds one comma too many or helpfully explains what it just did. Your n8n flow or your script tries to parse that, fails, and the whole chain stalls. This playbook shows you ten steps that get JSON out reliably. No magic, just craft.

Step 1, understand why it breaks in the first place

A language model produces text, not data structures. It has no built-in sense that a missing quotation mark breaks everything. To the model, "Sure, here's your JSON:" is a completely natural, friendly sentence. To your parser it's poison. The first step is to accept that you have to actively wean the model off its free-text behavior. It won't do that on its own.

Step 2, give the target format as an example, not as a description

Don't describe in words how the JSON should look, show it. That's few-shot prompting applied to structure. Attach a complete example object to the prompt, exactly the way you want it back:

Respond only with a JSON object in exactly this form:
{
  "request": "short sentence",
  "urgency": 3,
  "reply_needed": true
}

The model copies the form. A shown example beats three paragraphs of explanation, every time.

Step 3, say explicitly what should NOT come out

The most effective single sentence in your prompt is the ban on politeness. Write it in literally:

Output ONLY the JSON. No introductory text, no explanation,
no code fences, not a word before or after.

That sounds harsh and that's exactly what it should be. Most broken outputs come from a "Sure!" in front or an "I hope this helps" after. If you forbid it, it usually disappears.

Step 4, turn the creativity down

If your AI interface knows a temperature value, set it to 0 or close to it. Temperature controls how much the model rolls the dice. For creative writing you want dice rolls, for structured data you want the exact opposite, namely boring predictability. At temperature 0 the model always takes the most probable next building block, and with a clear format prompt the most probable is exactly your JSON.

Step 5, use your provider's JSON mode, if it has one

Many API providers now have a dedicated mode that guarantees valid JSON, often via a fixed schema. Exactly what this mode is called, which parameters it takes and whether your provider already has it changes constantly, so check the current API docs of your specific provider under the term structured output or JSON mode. If the mode exists, use it, because it's more reliable than any prompt trick. The prompt building blocks from this playbook still make sense, because they also take effect when you're working through the chat interface or a tool without this mode.

Step 6, put a validation step directly behind the AI

Never trust the output blindly. Right after the AI has answered, try to parse the result and check it against your expected schema. In n8n you do this with a code node, in a script with a JSON parse inside a try-catch. The question is simple: is this valid JSON and does it have the fields I expect. If yes, continue. If no, go to step 7.

Step 7, build a retry loop with the error message

This is the step that turns a shaky setup into a robust one. If the parsing fails, send the broken answer back to the AI together with the error message:

Your last answer was not valid JSON.
Error: {parser_error_message}
Output the answer again, this time as pure valid JSON,
without any further text.

Models are surprisingly good at correcting their own mistake when you show them the concrete error message. Limit the loop to two or three attempts, then abort and set the case aside for manual review. An endless loop burning money is worse than a failure.

Step 8, defuse the typical format traps in advance

A few classics that keep showing up. Code fences, meaning the three backticks with json in front, the model likes to wrap around the output. If you can't forbid them via the prompt, just cut them off in your validation step before you parse. Comments in JSON are invalid, forbid them explicitly. And German umlauts in field names sometimes cause trouble, keep your keys English and lowercase, the values may be German.

Step 9, test with the nasty cases, not the easy ones

Your setup passes on the standard email, sure. It gets interesting with the outliers. An empty input. An input in another language. An input that matches none of the expected categories. Build a test for each of these cases and see whether valid JSON still comes out. It's best to define in the prompt what should happen when the model is unsure, for example setting a field to null instead of guessing. Otherwise the AI invents a value and your automation processes garbage without noticing.

Step 10, log the raw outputs for the first few weeks

For the first two to three weeks, write down every raw AI answer, including the ones that passed through. That's how you see how often the retry loop really kicks in and which inputs cause problems. If, for example, you notice that 8 out of 100 answers needed a second attempt, you know your prompt still has room to improve. If it's 40 out of 100, something fundamental is wrong and you go back to step 2. Without this logging you're groping in the dark and only notice problems when a customer complains.

What's next

Once your JSON stands reliably, the logical next step is to hang it into a real automation. That's what Level 3 is for, especially the chapter Building AI into your automation. And if you don't want to retype the examples and prompt building blocks from this playbook every time, build yourself a prompt library, as described in the playbook of the same name. A reliable JSON prompt is worth its weight in gold, you don't want to invent it twice.

Source

The JSON and structured-output modes of the individual providers change frequently. Always check the concrete parameters in your provider's official docs before you hardwire them:

  • Anthropic Claude: https://docs.claude.com/en/docs/build-with-claude/tool-use/overview
  • OpenAI: https://platform.openai.com/docs/guides/structured-outputs

The prompt building blocks in this playbook are provider-independent and work even without a dedicated API mode, for example in the normal chat interface.

Reliable JSON from any AI, so your automation doesn't break — StudioMeyer Academy