JSON Mode and Structured Output: Getting Parseable Data from AI
Free-form AI text output is hard to process programmatically. This article covers how to use prompt engineering and parameter settings to get structured JSON data from AI.
Why Structured Output Is Needed
Free-form AI text is hard to parse programmatically. Structured output (JSON) lets you directly store AI results in databases, pass them to backends, or use them in frontend UIs without extra parsing.
JSON Mode Implementation
curl https://chinawhapi.com/v1/chat/completions -H "Authorization: Bearer {key}" -H "Content-Type: application/json" -d '{"model":"qwen3.6-plus",
"messages":[{"role":"user","content":"Extract person names, companies, and titles from the following text: {text}"}],
"response_format":{"type":"json_object"},
"messages":[{"role":"system","content":"Return in JSON format with name, company, and title fields"}]}'Prompt Design Tips
Clearly specify JSON structure and field types in the system prompt; provide output examples in the user prompt; ask the model to return null instead of fabricating when information can't be extracted.
Common Errors
Models sometimes return Markdown-wrapped JSON (e.g., ```json...```), requiring frontend strip processing. You can also explicitly ask in the prompt 'do not use markdown format, return only plain JSON text'.