Prompt template · Data
JSON extraction prompt for structured data
A good JSON extraction prompt gives the model the document and the exact shape you want, then tells it to use null for anything the text doesn’t state, copy names and amounts exactly, and return only the JSON. This template adds a notes field for ambiguities and pairs with your provider’s structured outputs.
Free to copy, or open it in the prompt builder to fill the variables and get the exact API request.
194 tokens unfilled · 335 with the example below · o200k_base (GPT-4o and later)
Template
The JSON extraction prompt template (copy it)
Paste this into a chat window and replace each {{variable}}, or open it in the prompt builder to fill them in a form. Sections are marked with XML tags, which every major model reads well.
You extract structured data from text accurately. You never guess a value that the text doesn’t state.
<context>
<document>
{{document}}
</document>
</context>
<constraints>
- Use only information stated in the document. If a field isn’t there, use null. Don’t infer it from context or general knowledge.
- Copy names, IDs and amounts exactly as written. Normalise only where the shape says so: dates to YYYY-MM-DD, amounts to numbers without currency symbols.
- If a value is ambiguous (two different dates could be the due date), use null and explain in the "notes" field.
- Include every key in the shape, even when its value is null.
</constraints>
<output_format>
Only the JSON object. No code fences, no text before or after it.
</output_format>
<task>
Extract the fields below from the document and return them as JSON matching this shape:
{{json_shape}}
</task>For an API call: system prompt and user message
With a system prompt, the role, constraints and output format stay fixed across calls (and can be cached), while the context and task go in the user message.
You extract structured data from text accurately. You never guess a value that the text doesn’t state.
<constraints>
- Use only information stated in the document. If a field isn’t there, use null. Don’t infer it from context or general knowledge.
- Copy names, IDs and amounts exactly as written. Normalise only where the shape says so: dates to YYYY-MM-DD, amounts to numbers without currency symbols.
- If a value is ambiguous (two different dates could be the due date), use null and explain in the "notes" field.
- Include every key in the shape, even when its value is null.
</constraints>
<output_format>
Only the JSON object. No code fences, no text before or after it.
</output_format><context>
<document>
{{document}}
</document>
</context>
<task>
Extract the fields below from the document and return them as JSON matching this shape:
{{json_shape}}
</task>Variables
What do the variables mean?
- {{document}}
- The source text: an email, an invoice, a PDF converted to text, a web page.
- {{json_shape}}
- The keys and types you want, as an example object or a JSON Schema. Include a "notes" string field for ambiguities.
Use
When should I use this JSON extraction prompt?
Use it to turn unstructured text into records: invoices into accounting rows, support emails into ticket fields, CVs into a candidate table, product pages into catalogue entries. It suits any pipeline where a program, not a person, reads the output.
In production, pair it with your provider’s structured outputs feature so the JSON is guaranteed to parse and match the schema. The prompt still matters: the schema controls the shape, while the instructions control whether the values are true.
Structure
Why is the prompt structured this way?
The strongest rule is “null instead of a guess”. Extraction models are tempted to fill every field, and a plausible invented due date is far worse than an empty one, because nothing downstream will catch it. Asking for every key, even when null, keeps the shape stable for the parser.
Copying values exactly, with normalisation only where specified, avoids subtle corruption: reformatted names, rounded amounts, invoice numbers with characters dropped. The notes field gives ambiguity somewhere to go instead of being silently resolved.
The output line asks for only the JSON, with no fences or commentary, so it can go straight into a parser when structured outputs aren’t available. If it still arrives wrapped in text, the JSON repair tool can extract and fix it, and the guide to invalid JSON from LLMs explains why it breaks.
Example
Example: a filled-in JSON extraction prompt
- {{document}}
From: accounts@northwind-supplies.example Subject: Invoice NW-20931 Hi, please find invoice NW-20931 for October. Total due: £1,284.50 (incl. VAT). Payment terms 30 days from the invoice date of 3 October 2026. PO ref: 77-1042.
- {{json_shape}}
{ "supplier_email": "string", "invoice_number": "string", "total": "number", "currency": "ISO 4217 code", "invoice_date": "YYYY-MM-DD", "due_date": "YYYY-MM-DD or null", "po_number": "string or null", "notes": "string or null" }
What to look for. The due date isn’t written, only implied (30 days from 3 October). Under the rules it should be null with a note explaining the terms, rather than a computed 2 November that your accounting system might treat as stated fact.
The complete filled-in prompt (335 tokens)
You extract structured data from text accurately. You never guess a value that the text doesn’t state.
<context>
<document>
From: accounts@northwind-supplies.example
Subject: Invoice NW-20931
Hi, please find invoice NW-20931 for October. Total due: £1,284.50 (incl. VAT).
Payment terms 30 days from the invoice date of 3 October 2026. PO ref: 77-1042.
</document>
</context>
<constraints>
- Use only information stated in the document. If a field isn’t there, use null. Don’t infer it from context or general knowledge.
- Copy names, IDs and amounts exactly as written. Normalise only where the shape says so: dates to YYYY-MM-DD, amounts to numbers without currency symbols.
- If a value is ambiguous (two different dates could be the due date), use null and explain in the "notes" field.
- Include every key in the shape, even when its value is null.
</constraints>
<output_format>
Only the JSON object. No code fences, no text before or after it.
</output_format>
<task>
Extract the fields below from the document and return them as JSON matching this shape:
{
"supplier_email": "string",
"invoice_number": "string",
"total": "number",
"currency": "ISO 4217 code",
"invoice_date": "YYYY-MM-DD",
"due_date": "YYYY-MM-DD or null",
"po_number": "string or null",
"notes": "string or null"
}
</task>Models
Does it work the same in ChatGPT, Claude and Gemini?
Yes, the structure works in all three: tagged sections that keep your material apart from the instructions, and an explicit output format. The tips below cover where the providers’ own guides differ for this JSON extraction prompt.
- OpenAI GPT
OpenAI’s Structured Outputs require every field in
required; make a field optional with a union such as["string", "null"], which fits the null-instead-of-guess rule exactly. - Claude
Anthropic’s structured outputs (
output_config.format) constrain Claude’s reply to your schema. Without it, the format line is what replaces the old prefill trick, which Claude 4.6 models and later reject with a 400 error. - Gemini
Gemini takes a schema with
response_formatfor structured output. Google warns that very large or deeply nested schemas may be rejected, so keep the shape flat where you can.
FAQ
Questions people ask
How do I get an LLM to return only JSON?
Use your provider’s structured outputs feature, which constrains the reply to a schema. The prompt still asks for only the JSON with no code fences, which helps when that feature isn’t available. If a reply still arrives wrapped in text, the JSON repair tool extracts and fixes it, and the invalid JSON guide explains why it breaks.
What should the model do with missing fields?
Return null. A plausible invented due date is far worse than an empty one, because nothing downstream will catch it. The template asks for every key in the shape even when its value is null, so the parser always sees the same fields, and puts ambiguities, such as two possible due dates, in a notes field.
Prompt or structured outputs: which should I use?
Both. Structured outputs control the shape: the JSON parses and matches your schema. The prompt controls whether the values are true: null instead of guesses, and exact copies of IDs and amounts. With OpenAI’s Structured Outputs, make a field optional with a type union of string and null, which fits the null rule.
Related
Related prompts and system prompts
- Classify textSort text into fixed labels with definitions, examples and an “unclear” option, returning JSON.
- Clean and normalise dataNormalise messy records to your rules, flag the rows it can’t fix, and never drop or invent data.
- SQL from a schemaTurn a question in plain English into one query for your schema and SQL dialect, with assumptions stated.
Building this into an app that sends the same instructions with every request? Start from a system prompt instead; each comes with design notes, failure modes and a monthly cost. In the system prompt library: JSON-only extraction service.
Edited the template? Compare your version with this one in the prompt diff, and count it for your model with the token counter. In the glossary: structured outputs, hallucination. Browse all 29 prompt templates.
Keep exploring