# How to force valid JSON output (no markdown)

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

Source: Promptea — https://www.promptea.me/en/guides/json-output · Language: English · Last updated: 2026-11-09

## 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

```text
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.
```

[Open in Promptea](https://www.promptea.me/en/analyzer?prompt=Extract+data+from+the+text+below.%0AReturn+ONLY+valid+JSON+%28no+markdown%2C+no+comments%29.%0A%0ASchema%3A%0A%7B%0A++%22people%22%3A+%5B%0A++++%7B%0A++++++%22name%22%3A+%22string%22%2C%0A++++++%22role%22%3A+%22string%7Cnull%22%2C%0A++++++%22email%22%3A+%22string%7Cnull%22%0A++++%7D%0A++%5D%2C%0A++%22dates%22%3A+%5B%22YYYY-MM-DD%22%5D%0A%7D%0A%0ARules%3A%0A-+If+missing%2C+use+null+%28do+not+invent%29.%0A-+If+a+date+is+ambiguous%2C+omit+it.%0A%0AText%3A%0A%22%22%22%5Bpaste+here%5D%22%22%22%0A%0ABefore+finalizing%2C+validate+that+the+output+is+valid+JSON+and+matches+the+schema.&purpose=data&target=gpt)

### Transform JSON to a new schema

```text
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.
```

[Open in Promptea](https://www.promptea.me/en/analyzer?prompt=You+will+receive+JSON+input.+Convert+it+to+the+output+schema.%0AReturn+ONLY+valid+JSON.%0A%0AInput+JSON%3A%0A%22%22%22%5Bpaste+JSON%5D%22%22%22%0A%0AOutput+schema%3A%0A%7B%0A++%22items%22%3A+%5B%0A++++%7B+%22id%22%3A+%22string%22%2C+%22title%22%3A+%22string%22%2C+%22tags%22%3A+%5B%22string%22%5D+%7D%0A++%5D%0A%7D%0A%0ARules%3A%0A-+Preserve+meaning%3B+do+not+invent+new+items.%0A-+If+an+input+field+doesn%27t+map%2C+ignore+it.%0A-+Ensure+tags+are+lowercase.%0A%0AValidate+JSON+before+output.&purpose=data&target=gemini)

## Frequently asked questions

### 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.
