Prompt
structured JSON output api
Latest observation
Nov 14, 2026
OpenAI APIWeb search: off
If you want an API to return structured JSON output, you usually mean one of these:
- Your API should always respond with valid JSON
- You want to enforce a specific JSON schema/shape
- You want an AI model/API to generate JSON only
Example of structured JSON response
{
"status": "success",
"data": {
"id": 123,
"name": "Alice",
"email": "alice@example.com"
}
}
Common ways to enforce structured JSON
1. In a normal web API
Return a JSON object from your backend:
- Node/Express
res.json({
status: "success",
data: { id: 123, name: "Alice" }
});
- Python/FastAPI
return {"status": "success", "data": {"id": 123, "name": "Alice"}}
2. With schema validation
Define a schema so the response must match it:
- JSON Schema
- Pydantic
- Zod
- OpenAPI/Swagger
3. For AI model output
If you’re using an LLM API, ask for JSON explicitly and validate it afterward. Some APIs also support structured outputs / JSON schema mode.
Example prompt:
Return only valid JSON with keys:
name,age,city.
Example output:
{"name":"Alice","age":30,"city":"London"}
Best practice
- Always set
Content-Type: application/json - Validate output against a schema
- Reject or repair invalid JSON
- Keep the JSON shape consistent
If you want, I can show you how to do this in:
- Python
- Node.js
- OpenAI API
- FastAPI / Express / Django / Flask