Back to all articles
7 MIN READ

How to Get JSON Output from ChatGPT and Other LLMs

By Learnia Team

How to Get JSON Output from ChatGPT and Other LLMs

This article is written in English. Our training modules are available in multiple languages.

📅 Last Updated: January 28, 2026

📚 Related: ChatGPT 5.2 Prompting Guide | Meta-Prompting Techniques | GPT-5.2-Codex Deep Dive


Table of Contents

  1. Why JSON Output Matters
  2. Basic Techniques
  3. Common Challenges
  4. Native JSON Mode
  5. Model Comparison
  6. Advanced Patterns
  7. FAQ

Getting AI to give you a nicely formatted JSON response instead of paragraphs of text can feel like magic—when it works. But it can also be frustrating when the model adds extra commentary or breaks the format.

Let's explore why structured output matters and the key concepts behind getting it right.


Master AI Prompting — €20 One-Time

10 ModulesLifetime Access
Get Full Access

Why JSON Output Matters

When you're building applications with AI, you don't want prose—you want data you can parse.

The Problem with Unstructured Output

User: List the top 3 programming languages

AI: Sure! Here are the top 3 programming languages:
1. Python - Great for beginners and data science
2. JavaScript - Essential for web development  
3. TypeScript - JavaScript with types

These are all excellent choices depending on your needs...

This is nice for humans but terrible for code. You can't reliably extract the data.

The Power of Structured Output

{
  "languages": [
    {"name": "Python", "use_case": "Data science, beginners"},
    {"name": "JavaScript", "use_case": "Web development"},
    {"name": "TypeScript", "use_case": "Type-safe JavaScript"}
  ]
}

Now your code can parse this directly. No regex gymnastics required.


Basic Techniques for JSON Output

1. Explicit Format Instructions

Tell the AI exactly what format you want:

Return your response as a JSON object with this structure:
{
  "title": "string",
  "summary": "string",
  "tags": ["array", "of", "strings"]
}

Only output the JSON, no other text.

2. Provide an Example

Show the AI what you expect:

Extract the product info from this description and return it as JSON.

Example output:
{"name": "Widget Pro", "price": 29.99, "category": "Tools"}

Description: "The SuperBlender 3000 costs $149 and is in our Kitchen category"

3. Use System Messages (API)

When using the API, the system message can enforce format:

System: You are a JSON-only responder. Output valid JSON and nothing else.

Common Challenges

1. Extra Text Around JSON

The model might say "Here's the JSON:" before the actual output.

Solution: Be explicit: "Output ONLY valid JSON. No introduction, no explanation."

2. Invalid JSON Syntax

Missing quotes, trailing commas, or broken brackets.

Solution: Ask the model to validate: "Ensure the JSON is valid and parseable."

3. Inconsistent Structure

Sometimes keys are present, sometimes they're not.

Solution: Define the schema explicitly and state which fields are required.


When Structured Output Shines

Structured output is essential for:

  • API integrations — Feeding AI output to other systems
  • Database storage — Storing responses in structured format
  • Automation workflows — Zapier, Make, n8n integrations
  • Frontend rendering — Displaying AI output in UI components
  • Data extraction — Pulling structured info from unstructured text

JSON vs. Other Formats

FormatBest ForLimitation
JSONAPIs, code integrationVerbose for simple data
MarkdownDocumentation, readable outputHarder to parse
CSVTabular dataNo nested structures
YAMLConfig files, human-readableLess common in APIs

JSON is the most universal choice for programmatic use.


Native JSON Mode (API)

Modern APIs now offer JSON Mode that guarantees valid JSON output. This is a game-changer for reliability.

OpenAI JSON Mode (GPT-5.2)

import openai

client = openai.OpenAI()

response = client.chat.completions.create(
    model="gpt-5.2",
    response_format={"type": "json_object"},
    messages=[
        {"role": "system", "content": "Output valid JSON only."},
        {"role": "user", "content": "List top 3 programming languages with use cases"}
    ]
)

# Guaranteed valid JSON
import json
data = json.loads(response.choices[0].message.content)

Claude JSON Mode

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-5-20250514",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": """Return a JSON object with this exact structure:
            {"languages": [{"name": "...", "use_case": "..."}]}
            
            List top 3 programming languages. Output ONLY the JSON."""
        }
    ]
)

Gemini Structured Output

Gemini supports schema-based structured output:

import google.generativeai as genai

model = genai.GenerativeModel('gemini-3-pro')

response = model.generate_content(
    "List top 3 programming languages",
    generation_config=genai.GenerationConfig(
        response_mime_type="application/json",
        response_schema={
            "type": "object",
            "properties": {
                "languages": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "name": {"type": "string"},
                            "use_case": {"type": "string"}
                        }
                    }
                }
            }
        }
    )
)

JSON Output by Model

ModelJSON ModeSchema ValidationReliability
GPT-5.2✅ NativeVia function callingExcellent
GPT-4o✅ NativeVia function callingVery Good
Claude Sonnet 4.5✅ NativeVia tool useExcellent
Gemini 3 Pro✅ Native✅ Schema-basedExcellent
DeepSeek V3⚠️ PromptingManualGood
Llama 3⚠️ PromptingManualVariable

Advanced JSON Patterns

Pattern 1: Schema Definition

Define your schema explicitly for consistent results:

Return a JSON object matching this TypeScript interface:

interface ProductAnalysis {
  name: string;
  category: string;
  sentiment: "positive" | "negative" | "neutral";
  keyFeatures: string[];  // exactly 3 items
  priceRange: {
    min: number;
    max: number;
    currency: "USD" | "EUR";
  };
}

Pattern 2: Validation Instructions

Ask the model to self-validate:

Before outputting, verify your JSON:
1. All required fields present
2. Arrays have correct length
3. No trailing commas
4. Strings properly quoted
5. Valid JSON.parse() compatible

If any check fails, fix before outputting.

Pattern 3: Error Recovery

Handle malformed JSON gracefully:

import json
import re

def parse_llm_json(response: str) -> dict:
    # Try direct parse
    try:
        return json.loads(response)
    except json.JSONDecodeError:
        pass
    
    # Extract JSON from markdown code block
    match = re.search(r'```json?\s*([\s\S]*?)```', response)
    if match:
        try:
            return json.loads(match.group(1))
        except json.JSONDecodeError:
            pass
    
    # Extract first { } block
    match = re.search(r'\{[\s\S]*\}', response)
    if match:
        try:
            return json.loads(match.group(0))
        except json.JSONDecodeError:
            pass
    
    raise ValueError("Could not parse JSON from response")

FAQ

Does JSON mode cost more tokens?

No. JSON mode doesn't increase token usage. However, requesting structured output often produces more concise responses.

Can I use JSON mode with streaming?

Yes, but you'll need to buffer the response and parse once complete, as partial JSON isn't valid.

What if the model returns invalid JSON despite JSON mode?

With native JSON mode, this is extremely rare. If it happens, retry the request. For critical applications, implement validation and retry logic.

Should I use function calling instead of JSON mode?

Function calling (tool use) is better when you need the model to choose between actions. JSON mode is better for pure data extraction.



Key Takeaways

  1. Structured output makes AI responses machine-readable
  2. Use explicit format instructions and examples
  3. State "JSON only, no other text" to prevent extras
  4. Define your schema clearly for consistency
  5. JSON Mode in modern APIs provides guarantees

Ready to Master Structured Outputs?

This article covered the what and why of getting JSON from AI. But reliable structured output requires deeper techniques.

In our Module 2 — Structured Outputs, you'll learn:

  • Advanced schema definition techniques
  • How to handle optional vs. required fields
  • Validation strategies for production systems
  • Working with nested and complex structures
  • Using JSON Mode and function calling APIs

Explore Module 2: Structured Outputs


Last Updated: January 28, 2026

GO DEEPER

Module 2 — Structured Outputs

Learn to get reliable, formatted responses like JSON and tables.