Advanced Prompt Engineering / Advanced Prompt Structures

JSON Prompting

Intermediate [3/5]
JSON-style prompts

Definition

JSON prompting uses JSON format to structure prompts and specify expected output formats. This approach is ideal for applications that need to parse LLM outputs programmatically, ensuring consistent, machine-readable responses.

Combined with JSON mode in modern APIs, this technique guarantees valid JSON output every time.

Key Concepts

  • Schema definition: Specifying expected JSON structure
  • Type hints: Indicating expected data types for fields
  • Example-driven: Showing sample JSON output
  • Parseable output: Guaranteed valid JSON for downstream processing

Examples

Output Specification
Define Expected Structure
Extract information and respond in this JSON format: { "person_name": "string", "age": number, "occupation": "string", "skills": ["string", "string"], "is_employed": boolean } Text: "John Smith, 34, is a software engineer skilled in Python and JavaScript. He currently works at TechCorp."
Complete Example
Show Expected Output
Example input: "Alice, 28, teaches math at Lincoln High" Example output: { "name": "Alice", "age": 28, "profession": "teacher", "subject": "math", "workplace": "Lincoln High" } Now process: "Bob, 45, practices law at Smith & Associates"

Interactive Exercise

Design JSON Output

Design a JSON schema for extracting recipe information:

Include: name, prep time, ingredients list, and difficulty level

Pro Tips
  • Always enable JSON mode in API when expecting JSON
  • Provide a complete example to reduce format errors
  • Use snake_case or camelCase consistently
  • Validate output against schema before using

Related Terms