Prompt

How do I get structured JSON output from an AI API?

Artificial Intelligence / AI Platforms1 observationLast seen Oct 8, 2026

Latest observation

Oct 8, 2026OpenAI APIWeb search: off

To get structured JSON output from an AI API, the usual goal is to make the model return data that matches a schema you define, instead of free-form text.

Best options

1) Use the API’s native JSON / structured output mode

If your AI provider supports it, this is the most reliable approach.

Typical patterns:

  • JSON mode: forces valid JSON only
  • Structured outputs / schema mode: forces JSON that matches a schema
  • Function/tool calling: model returns arguments in a structured format

Example idea:

{
  "name": "Alice",
  "age": 32,
  "skills": ["python", "api", "ml"]
}

If the API supports a schema, define something like:

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" },
    "skills": {
      "type": "array",
      "items": { "type": "string" }
    }
  },
  "required": ["name", "age", "skills"],
  "additionalProperties": false
}

2) Prompt the model very explicitly

If structured mode is not available, use a strong prompt:

Example:

Return only valid JSON. Do not include markdown, explanations, or extra text.
Use this schema:
{ "name": string, "age": number, "skills": string[] }

This helps, but it is less reliable than native schema enforcement.

3) Validate and retry

Always:

  • parse the output as JSON
  • validate against your schema
  • if invalid, retry with a correction prompt

This makes the system much more robust.


Practical example

Say you want extracted info from text:

Input:

“John is 29 and knows JavaScript and Go.”

Desired output:

{
  "name": "John",
  "age": 29,
  "skills": ["JavaScript", "Go"]
}

Prompt:

Extract the person's name, age, and skills.
Return only valid JSON matching:
{
  "name": "string",
  "age": "number",
  "skills": ["string"]
}

Tips to improve reliability

  • Keep schemas simple
  • Set temperature = 0 for consistency
  • Specify additionalProperties: false if supported
  • Ask for arrays/objects explicitly
  • Don’t ask for prose and JSON in the same response
  • Post-process with a JSON parser and schema validator

If you tell me your API/provider

I can give you a copy-paste example for:

  • OpenAI
  • Anthropic
  • Gemini
  • Azure OpenAI
  • local models / Ollama
  • generic REST API

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.