How to force valid JSON output (no markdown)

Templates to extract and transform data into strict JSON, with validation rules.

Last updated: · Markdown version

Rules that work

  • Tell it: 'Output ONLY JSON' and forbid extra text.
  • Define a schema (keys, types, allowed values).
  • State what to do on missing fields (null vs omit).
  • Ask for a second pass: validate + fix JSON.

Before → after: from 'give me JSON' to a contract

  • Before: "Give me the contact data from this email as JSON." Typical result: a sentence of introduction, a markdown code fence, and keys that change from run to run.
  • After: "Extract the contact data. Return ONLY valid JSON, no markdown. Schema: {name: string, email: string|null, phone: string|null}. Use null for anything missing; do not guess."
  • What changed: the output is a defined schema with a rule for missing values and a ban on extra text, so the same prompt gives the same shape every time.

Validate in code, not by hope

  • Parse the response with a real JSON parser and check it against the schema (required keys, types, allowed values) before using it.
  • On failure, retry once with the error attached: 'Your previous output failed validation: [error]. Return corrected JSON only.'
  • Where the model or API offers a native structured-output mode, use it: the format is then enforced at generation time instead of requested in prose. Promptea's model pages show which current models list it.
  • Valid JSON is not necessarily correct content. Spot-check values against the source text, especially numbers and dates.

Templates

Extract structured JSON

Extract data from the text below.
Return ONLY valid JSON (no markdown, no comments).

Schema:
{
  "people": [
    {
      "name": "string",
      "role": "string|null",
      "email": "string|null"
    }
  ],
  "dates": ["YYYY-MM-DD"]
}

Rules:
- If missing, use null (do not invent).
- If a date is ambiguous, omit it.

Text:
"""[paste here]"""

Before finalizing, validate that the output is valid JSON and matches the schema.
Opens the analyzer with the prompt prefilled.
Open in Promptea

Transform JSON to a new schema

You will receive JSON input. Convert it to the output schema.
Return ONLY valid JSON.

Input JSON:
"""[paste JSON]"""

Output schema:
{
  "items": [
    { "id": "string", "title": "string", "tags": ["string"] }
  ]
}

Rules:
- Preserve meaning; do not invent new items.
- If an input field doesn't map, ignore it.
- Ensure tags are lowercase.

Validate JSON before output.
Opens the analyzer with the prompt prefilled.
Open in Promptea

FAQ

Why does the model still add text?
Because you didn't make it costly. Repeat the constraint, provide a schema, and ask for validation.
Should I use JSON Schema?
Yes for strict integrations. Even a simplified schema (keys + types) helps a lot.
What should the model return when a value is missing?
Pick one rule and state it: null for a missing value or omit the key. Mixing the two forces your code to handle both. Also tell it not to guess.
Can I ask for JSON and an explanation in the same answer?
Keep them apart: put the explanation in a dedicated key such as "notes", or make a second request. Text outside the JSON breaks parsing.