Structured JSON Output with ChatGPT for Dynamic App Development
Learn how to generate structured JSON, tables, or HTML using ChatGPT’s JSON mode. Ideal for apps that parse forms, templates, or summarize structured content.
When you’re building a tool that needs clean and structured data from ChatGPT—whether it’s a form filler, report generator, or code assistant—ChatGPT’s JSON mode comes in handy.
Instead of returning free-form text, you can instruct the model to respond strictly in JSON format. This makes parsing in your app much easier and safer.
What is JSON Mode?
OpenAI’s Chat Completions API now supports a mode where responses are guaranteed to be valid JSON. It eliminates the need to manually clean or regex the text.
Use cases include:
-
Converting unstructured content into structured JSON
-
Summarizing blog posts into schema-ready metadata
-
Filling predefined templates (invoices, resumes, reports)
-
Building API response mockers or validators
Prompt Format Example
You simply tell ChatGPT to “Respond only in JSON”, and define a structure:
You are an assistant that returns structured data. Output the following JSON:
{
"title": string,
"summary": string,
"tags": string[]
}
Calling ChatGPT API with JSON Mode in Go
Install SDK
go get github.com/sashabaranov/go-openai
Code Example
package main
import (
"context"
"fmt"
"log"
"os"
openai "github.com/sashabaranov/go-openai"
)
func main() {
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
req := openai.ChatCompletionRequest{
Model: "gpt-4",
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a strict JSON responder.",
},
{
Role: openai.ChatMessageRoleUser,
Content: `Extract the following in JSON:
{
"title": string,
"summary": string,
"tags": string[]
}
Content: ChatGPT helps developers generate clean data formats.`,
},
},
ResponseFormat: openai.ChatCompletionResponseFormatJSON,
}
resp, err := client.CreateChatCompletion(context.Background(), req)
if err != nil {
log.Fatal("Error generating JSON:", err)
}
fmt.Println("Structured JSON:\n", resp.Choices[0].Message.Content)
}
This will return something like:
{
"title": "Structured Data with ChatGPT",
"summary": "ChatGPT helps developers generate clean data formats.",
"tags": ["chatgpt", "json", "developer"]
}
Use Cases in Apps
Form Extraction
{
"name": "Harendra Singh",
"skills": ["Go", "SQL", "GCP"],
"experience_years": 3
}
Blog Summarizer
Parse blog content and extract:
{
"title": "...",
"excerpt": "...",
"seo_keywords": ["golang", "ai", "json"]
}
Image Prompt Generator
Convert input like: “a cat riding a bike on Mars”
{
"subject": "cat",
"action": "riding",
"location": "Mars",
"style": "sci-fi"
}
Tips for Stable Output
-
Always provide the desired JSON schema in the prompt
-
Use response_format = json (only supported in official OpenAI clients)
-
Handle the response as string and decode using Go’s encoding/json