JSON Prompting: How to Get Consistent Outputs from LLMs
Learn what JSON prompting is, why it produces predictable AI responses, and how to write effective structured prompts with simple, practical examples.
Large language models are deterministic systems. Given the same input, they produce similar outputs. Yet anyone who has worked with ChatGPT, Claude, or Gemini knows this is not how it feels in practice. You ask the same question twice and get two completely different responses in two completely different formats.
Why does this happen? English is a loosely typed language. Every sentence can be interpreted in multiple ways. When the model has to guess your intent, it produces inconsistent results. One day you get a paragraph, the next day a bulleted list, and the day after that a table.
JSON prompting solves this problem. Instead of writing vague instructions and hoping for the best, you tell the model exactly what structure you want. The result is predictable, consistent output every time.
What is JSON Prompting?
JSON prompting is simply asking the AI to return its response in a specific JSON format. You define the fields you want, and the model fills them in.
Here is a regular prompt:
Tell me about the weather in Tokyo.
The model might respond with a long paragraph, a short sentence, or a bulleted list. You have no control over the format.
Here is the same request as a JSON prompt:
What is the weather in Tokyo? Respond in this JSON format:
{
"city": "Tokyo",
"temperature": <number>,
"condition": "<sunny/cloudy/rainy>"
}
Now the model knows exactly what you want. It will return something like:
{
"city": "Tokyo",
"temperature": 18,
"condition": "sunny"
}
That is JSON prompting. You define the shape of the answer, and the model fills it in.
Why Use JSON Prompting?
There are three main reasons to use JSON prompting.
Consistency. Every response follows the same structure. If you ask the same question 100 times, you get 100 responses in the same format. This matters when you are building applications or processing many requests.
Easier parsing. JSON can be read directly by code. If your application needs to use the AI's response, structured JSON is much easier to work with than free-form text.
Less ambiguity. When you specify exact fields, the model cannot wander off topic or include irrelevant information. It answers exactly what you asked.
When Should You Use It?
JSON prompting works best when:
- You need the same format every time
- Your code will process the response
- You want specific pieces of information, not general explanations
- You are extracting data from text
JSON prompting is not ideal for:
- Creative writing (stories, poems, essays)
- Open-ended conversations
- Simple questions where a plain answer works fine
How to Write a JSON Prompt
Writing a JSON prompt is straightforward. Follow these four steps.
Step 1: State What You Want
Start with a clear instruction. Tell the model what task to perform.
Analyze this product review.
Summarize this email.
Extract information from this text.
Keep it simple and direct.
Step 2: Provide the Input
Give the model the data to work with. Mark it clearly so there is no confusion about what is instruction and what is data.
Review: "Great laptop, but the battery only lasts 3 hours."
Or use quotes or labels to separate it:
Text to analyze:
"""
Great laptop, but the battery only lasts 3 hours.
"""
Step 3: Define the Output Format
Show the model exactly what JSON structure you want. Use placeholders to indicate what goes where.
{
"sentiment": "<positive/negative/mixed>",
"pros": ["<string>"],
"cons": ["<string>"]
}
The placeholders like <positive/negative/mixed> tell the model what kind of value to put there.
Step 4: Add a JSON-Only Instruction
Tell the model to return only JSON, nothing else.
Return only valid JSON. No other text.
This prevents the model from adding explanations before or after the JSON.
Putting It Together
Here is a complete JSON prompt:
Analyze this product review and return JSON.
Review: "Great laptop, but the battery only lasts 3 hours."
Format:
{
"sentiment": "<positive/negative/mixed>",
"pros": ["<string>"],
"cons": ["<string>"]
}
Return only valid JSON.
The model responds:
{
"sentiment": "mixed",
"pros": ["Great laptop"],
"cons": ["Battery only lasts 3 hours"]
}
Clean, structured, and predictable.
Simple Examples You Can Use Today
Here are practical examples you can copy and modify for your own use. Each one is short enough to type out yourself.
Example 1: Sentiment Analysis
What is the sentiment of this text?
Text: "I love this app but it crashes sometimes."
{
"sentiment": "<positive/negative/mixed>",
"reason": "<brief explanation>"
}
Example 2: Task Extraction
Extract tasks from this message.
Message: "Please send the report by Friday and schedule a meeting with the team."
{
"tasks": ["<task1>", "<task2>"]
}
Example 3: Content Summary
Summarize this article in JSON.
Article: "Apple announced a new iPhone today with better cameras and longer battery life. The price starts at $999."
{
"topic": "<string>",
"key_points": ["<point1>", "<point2>"],
"mentioned_price": "<price or null>"
}
Example 4: Classification
Classify this support ticket.
Ticket: "My order hasn't arrived and it's been two weeks."
{
"category": "<shipping/billing/technical/other>",
"urgency": "<low/medium/high>"
}
Example 5: Data Extraction
Extract contact info from this text.
Text: "Reach me at john@example.com or call 555-1234."
{
"email": "<email or null>",
"phone": "<phone or null>"
}
These examples are simple enough to memorize and adapt to your needs.
JSON Prompting for Images
JSON prompting also works for image generation. Instead of writing a long description and hoping the AI understands, you can structure your request.
Basic Image Prompt
A simple natural language prompt:
A cat sitting on a windowsill
The same idea as a JSON prompt:
Generate an image with these details:
{
"subject": "orange tabby cat",
"action": "sitting",
"location": "wooden windowsill",
"background": "sunny garden visible through window",
"style": "photorealistic"
}
The structured version gives you more control over each element.
Product Photo Prompt
Create a product photo:
{
"product": "white coffee mug",
"surface": "marble countertop",
"lighting": "soft natural light",
"style": "minimalist",
"angle": "slight overhead"
}
Character Design Prompt
Design a character:
{
"type": "fantasy warrior",
"gender": "female",
"armor": "silver plate mail",
"weapon": "longsword",
"pose": "standing guard",
"art_style": "digital painting"
}
The benefit here is consistency. If you need multiple images of the same character, the JSON format helps you keep details consistent across generations.
JSON Prompting for Code
When asking AI to write code, JSON prompts help you specify exactly what you need. Here are practical examples for common coding tasks.
Function Generation
Instead of a vague request, specify exactly what you need:
Write a function with these specs:
{
"name": "validateEmail",
"language": "TypeScript",
"input": "email: string",
"output": "boolean",
"checks": ["has @ symbol", "has domain", "no spaces"]
}
Return only the code.
Bug Analysis
When debugging, get structured feedback:
Analyze this code for bugs:
Code:
const data = fetch('/api/users');
console.log(data.json());
{
"has_bugs": <true/false>,
"issues": ["<issue description>"],
"fixed_code": "<corrected code>"
}
Code Explanation
Get explanations in a structured format:
Explain this code:
const result = arr.filter(x => x > 5).map(x => x * 2);
{
"summary": "<one sentence>",
"steps": ["<step1>", "<step2>"],
"output_type": "<string>"
}
Regex Generation
Create a regex for this requirement:
Requirement: "Match email addresses"
{
"pattern": "<regex>",
"explanation": "<what it matches>",
"example_matches": ["<example1>", "<example2>"],
"example_non_matches": ["<example1>"]
}
API Response Design
Design a response for this API:
Endpoint: GET /users/:id
{
"success_response": {
"status": <number>,
"body": "<structure>"
},
"error_response": {
"status": <number>,
"body": "<structure>"
}
}
Test Case Generation
Generate test cases for this function:
Function: calculateDiscount(price: number, percent: number): number
{
"test_cases": [
{
"input": "<values>",
"expected": <result>,
"description": "<what it tests>"
}
]
}
Database Query
Write a SQL query:
Requirement: "Get all users who signed up last month"
Table: users (id, name, email, created_at)
{
"query": "<SQL>",
"explanation": "<what it does>"
}
These prompts work because they give the model clear specifications. Instead of interpreting what you might want, the model follows your exact requirements.
Using API Features
Many AI APIs now have built-in JSON modes. When building applications, you can combine your JSON prompt with these features for extra reliability. The prompt defines what structure you want, and the API ensures valid JSON output.
OpenAI (TypeScript)
import OpenAI from "openai";
const openai = new OpenAI();
const prompt = `
Analyze this review:
"Great product but shipping was slow."
{
"sentiment": "<positive/negative/mixed>",
"pros": ["<string>"],
"cons": ["<string>"]
}
Return only valid JSON.
`;
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: prompt }],
response_format: { type: "json_object" },
});
const result = JSON.parse(response.choices[0].message.content);
console.log(result);
The response_format: { type: "json_object" } setting tells OpenAI to enforce valid JSON output. Combined with your prompt that defines the structure, you get reliable, parseable results.
Anthropic Claude (TypeScript)
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic();
const prompt = `
Analyze this review:
"Great product but shipping was slow."
{
"sentiment": "<positive/negative/mixed>",
"pros": ["<string>"],
"cons": ["<string>"]
}
Return only valid JSON.
`;
const response = await anthropic.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
messages: [{ role: "user", content: prompt }],
});
const text = response.content[0].type === "text" ? response.content[0].text : "";
const result = JSON.parse(text);
console.log(result);
Claude follows JSON formatting instructions reliably. For even stricter control, you can prefill the assistant response to start with {:
const response = await anthropic.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
messages: [
{ role: "user", content: prompt },
{ role: "assistant", content: "{" }
],
});
// Prepend the "{" we used to prefill
const text = response.content[0].type === "text" ? response.content[0].text : "";
const result = JSON.parse("{" + text);
This technique forces the model to continue from an opening brace, ensuring it outputs JSON immediately without any preamble.
Parsing and Validation
Regardless of which API you use, always validate the response:
function parseResponse<T>(content: string): T | null {
try {
// Remove markdown code blocks if present
const cleaned = content
.replace(/```json\n?/g, "")
.replace(/```\n?/g, "")
.trim();
return JSON.parse(cleaned) as T;
} catch (error) {
console.error("Failed to parse JSON:", error);
return null;
}
}
// Usage
interface ReviewAnalysis {
sentiment: "positive" | "negative" | "mixed";
pros: string[];
cons: string[];
}
const result = parseResponse<ReviewAnalysis>(responseText);
if (result) {
console.log(`Sentiment: ${result.sentiment}`);
console.log(`Pros: ${result.pros.join(", ")}`);
}
Using TypeScript interfaces gives you type safety after parsing. Your editor can autocomplete fields and catch errors at compile time.
Tips for Better Results
Start simple. Begin with 2-3 fields. Add more only if needed.
Use clear field names. Names like sentiment and category are better than s or cat.
Specify allowed values. Writing <positive/negative/mixed> is clearer than just <string>.
Handle missing data. Tell the model what to do when information is not available:
{
"phone": "<phone number or null if not found>"
}
Always say JSON only. End your prompt with "Return only valid JSON" to prevent extra text.
Test with variations. Try your prompt with different inputs to make sure it works consistently.
Common Mistakes
Too many fields. If your JSON has 20 fields, the model is more likely to make mistakes. Keep it focused.
Vague placeholders. Writing <value> does not help. Be specific: <number>, <string>, <true/false>.
Forgetting the JSON-only instruction. Without it, models often add explanations like "Here is the JSON you requested:" before the actual JSON.
Nested structures when flat works. This is harder to get right:
{
"author": {
"name": "...",
"email": "..."
}
}
This is easier:
{
"author_name": "...",
"author_email": "..."
}
Use nesting only when it genuinely makes sense.
Conclusion
JSON prompting is a simple technique with significant benefits. By defining the exact structure you want, you transform unpredictable AI responses into consistent, usable data.
The approach is straightforward:
- State what you want
- Provide the input
- Define the JSON format
- Ask for JSON only
Start with the simple examples in this article. Modify them for your use case. As you get comfortable, you can add more fields and handle more complex scenarios.
The key is to begin simply. A JSON prompt with three fields that works reliably is more valuable than a complex prompt with twenty fields that fails half the time.
Whether you are analyzing text, generating images, writing code, or building applications, JSON prompting gives you control over AI outputs. Try it on your next project and see the difference consistency makes.