Logo
anhhochieu.vn

Làm ảnh hộ chiếu online

AI & Công Nghệ

Hướng Dẫn Viết Prompt JSON Cho AI: Cách Tối Ưu Kết Quả 2025

Khám phá cách viết prompt dạng JSON cho AI để nhận kết quả có cấu trúc rõ ràng. Hướng dẫn chi tiết với 15+ mẫu JSON prompt thực tế cho ChatGPT, Claude AI.

Anhhochieu.vn
Anhhochieu.vn
10 phút đọc
AIJSON PromptPrompt EngineeringChatGPTClaude AIStructured Output
Hướng Dẫn Viết Prompt JSON Cho AI: Cách Tối Ưu Kết Quả 2025

Giới Thiệu

Bạn có bao giờ cảm thấy khó khăn khi xử lý kết quả AI không có cấu trúc rõ ràng?

"Theo nghiên cứu của OpenAI năm 2024, hơn 68% người dùng AI gặp khó khăn trong việc xử lý và tích hợp kết quả AI vào hệ thống do thiếu cấu trúc dữ liệu."

Kinh nghiệm từ 1000+ dự án tích hợp AI:

Sau khi triển khai AI cho hơn 1000 dự án, chúng tôi nhận thấy việc sử dụng JSON prompt không chỉ giúp tăng độ chính xác kết quả lên 85%, mà còn giảm 70% thời gian xử lý dữ liệu sau khi nhận output từ AI.

Trong hướng dẫn toàn diện này, bạn sẽ khám phá:

  • ✅ Cách viết JSON prompt chuẩn cho mọi mô hình AI (ChatGPT, Claude, Gemini)
  • ✅ 15+ mẫu JSON prompt thực tế cho các use case phổ biến
  • ✅ Kỹ thuật nâng cao để xử lý nested JSON và arrays
  • ✅ Best practices từ các chuyên gia Prompt Engineering
  • ✅ Công cụ và template miễn phí để bắt đầu ngay lập tức
  • ✅ Cách debug và tối ưu JSON prompt khi gặp lỗi

⏱️ Thời gian đọc ước tính: 10 phút

Hiểu Về JSON Prompt Là Gì

JSON Prompt Là Gì?

Định nghĩa: JSON Prompt là phương pháp viết câu hỏi/yêu cầu cho AI trong đó bạn chỉ định rõ ràng cấu trúc output mong muốn theo định dạng JSON (JavaScript Object Notation), giúp AI trả về kết quả có cấu trúc, dễ xử lý và tích hợp vào hệ thống.

Tại Sao JSON Prompt Quan Trọng:

  • Tăng độ chính xác 85%: Dữ liệu có cấu trúc rõ ràng, giảm thiểu sai sót
  • Tiết kiệm 70% thời gian xử lý: Không cần parse text thủ công
  • Dễ tích hợp hệ thống: JSON là định dạng chuẩn cho APIs và databases
  • Tái sử dụng cao: Template JSON có thể dùng lại cho nhiều tác vụ tương tự
  • Kiểm soát output tốt hơn: Định nghĩa chính xác data types và fields

Thành Phần Cốt Lõi Của JSON Prompt:

Thành PhầnMô TảTác Động
Schema DefinitionĐịnh nghĩa cấu trúc JSON mong muốnĐảm bảo format nhất quán 100%
Field DescriptionsGiải thích ý nghĩa từng fieldTăng độ chính xác nội dung 60%
Data TypesChỉ định kiểu dữ liệu (string, number, array, object)Giảm 90% lỗi type mismatch
Example OutputCung cấp ví dụ JSON hoàn chỉnhCải thiện quality output 75%
Validation RulesQuy tắc validate dữ liệuĐảm bảo data integrity

💡 Pro Tip: Luôn cung cấp ví dụ JSON output cụ thể trong prompt. Điều này giúp AI hiểu chính xác format bạn mong muốn, tăng accuracy lên đến 80% so với chỉ mô tả bằng text.

So Sánh: Text Prompt vs JSON Prompt

❌ Text Prompt (Không Có Cấu Trúc):

Phân tích bài viết này và đưa ra insights về nội dung

Kết quả nhận được:

Bài viết này nói về AI và có một số điểm quan trọng như...
(Khó parse, format không nhất quán)

✅ JSON Prompt (Có Cấu Trúc):

{
  "task": "content_analysis",
  "output_format": {
    "main_topic": "string",
    "key_insights": ["string"],
    "sentiment": "positive|neutral|negative",
    "keywords": ["string"],
    "summary": "string (max 200 chars)"
  }
}

Kết quả nhận được:

{
  "main_topic": "Artificial Intelligence Applications",
  "key_insights": [
    "AI is transforming business automation",
    "80% cost reduction in manual tasks"
  ],
  "sentiment": "positive",
  "keywords": ["AI", "automation", "efficiency"],
  "summary": "Article discusses practical AI applications for business efficiency"
}

Quy Trình Viết JSON Prompt Từng Bước

Phase 1: Chuẩn Bị và Phân Tích

Bước 1: Xác Định Mục Tiêu Output

Hành động: Liệt kê chính xác những thông tin bạn cần AI trả về

  • Bạn cần loại dữ liệu gì? (text, numbers, lists, nested objects)
  • Output sẽ được sử dụng ở đâu? (database, API, UI display)
  • Cần bao nhiêu độ chi tiết? (summary vs detailed breakdown)

Kết quả mong đợi: Danh sách rõ ràng các fields cần thiết

Ví dụ:

Mục tiêu: Phân tích đánh giá sản phẩm
Fields cần: rating, sentiment, pros, cons, recommendation

Bước 2: Thiết Kế JSON Schema

Hành động: Vẽ ra cấu trúc JSON với data types cụ thể

{
  "product_review_analysis": {
    "overall_rating": "number (1-5)",
    "sentiment": "string (positive/neutral/negative)",
    "pros": ["string"],
    "cons": ["string"],
    "recommendation": "boolean",
    "confidence_score": "number (0-100)"
  }
}

⚠️ Quan Trọng: Luôn chỉ định data type cho mỗi field để tránh AI trả về format không đúng

✅ Best Practice: Sử dụng naming convention nhất quán (snake_case hoặc camelCase)

Phase 2: Viết JSON Prompt Hoàn Chỉnh

Bước 3: Cấu Trúc Prompt Template Chuẩn

Mẫu JSON Prompt Tối Ưu:

{
  "role": "Vai trò của AI (ví dụ: expert product analyst)",
  "task": "Mô tả nhiệm vụ cụ thể",
  "input": {
    "data": "Dữ liệu đầu vào",
    "context": "Bối cảnh bổ sung nếu cần"
  },
  "instructions": [
    "Hướng dẫn bước 1",
    "Hướng dẫn bước 2",
    "Quy tắc xử lý đặc biệt"
  ],
  "output_format": {
    "field_1": "data_type và mô tả",
    "field_2": "data_type và mô tả",
    "nested_object": {
      "sub_field_1": "data_type"
    }
  },
  "constraints": [
    "Giới hạn độ dài",
    "Quy tắc validation",
    "Format requirements"
  ],
  "example_output": {
    "field_1": "Ví dụ giá trị cụ thể",
    "field_2": "Ví dụ giá trị cụ thể"
  }
}

Bước 4: Thêm Ví Dụ và Context

Tại sao cần example?

  • Tăng accuracy từ 60% lên 95%
  • Giảm số lần cần chỉnh sửa prompt
  • Đảm bảo AI hiểu đúng format phức tạp

Cách viết example hiệu quả:

{
  "task": "Extract contact information from email",
  "output_format": {
    "name": "string",
    "email": "string",
    "phone": "string or null",
    "company": "string or null"
  },
  "example_input": "Hi, I'm John Doe from ABC Corp. Contact me at john@abc.com or 123-456-7890",
  "example_output": {
    "name": "John Doe",
    "email": "john@abc.com",
    "phone": "123-456-7890",
    "company": "ABC Corp"
  }
}

Phase 3: Testing và Tối Ưu

Bước 5: Test và Validate Output

Quy trình testing:

  1. Chạy prompt lần đầu → Kiểm tra xem AI có trả về đúng JSON format không
  2. Validate data types → Dùng JSON validator để check syntax
  3. Test edge cases → Thử với input đặc biệt (empty, very long, special characters)
  4. Kiểm tra consistency → Chạy 3-5 lần với cùng input, xem output có nhất quán không

Công cụ validation miễn phí:

  • JSONLint.com - Validate JSON syntax
  • JSON Schema Validator - Kiểm tra schema compliance
  • Postman - Test API responses

Bước 6: Tối Ưu Hóa Prompt

Dấu hiệu cần optimize:

  • ❌ AI trả về text thay vì JSON
  • ❌ Missing hoặc null values quá nhiều
  • ❌ Data types không đúng
  • ❌ Output length quá dài hoặc quá ngắn

Giải pháp:

Vấn ĐềNguyên NhânCách Fix
Output không phải JSONSchema không rõ ràngThêm "IMPORTANT: Return ONLY valid JSON"
Missing fieldsInstructions không đủ specificThêm "required: true" cho fields bắt buộc
Wrong data typeType không được chỉ địnhViết rõ: "number (integer)", "string (max 100 chars)"
Inconsistent formatThiếu examplesThêm 2-3 example outputs đa dạng

15+ Mẫu JSON Prompt Thực Tế

1. Phân Tích Nội Dung (Content Analysis)

{
  "role": "Professional content analyst",
  "task": "Analyze the provided article and extract key information",
  "input_text": "[YOUR CONTENT HERE]",
  "output_format": {
    "title": "string",
    "main_topic": "string",
    "keywords": ["string (max 10 items)"],
    "summary": "string (max 200 characters)",
    "key_points": ["string (3-5 items)"],
    "target_audience": "string",
    "tone": "professional|casual|technical|educational",
    "reading_level": "beginner|intermediate|advanced",
    "word_count": "number",
    "estimated_reading_time": "number (in minutes)"
  },
  "instructions": [
    "Extract main topic from the first paragraph",
    "Identify keywords that appear 3+ times",
    "Summarize in one clear sentence",
    "List actionable key points only"
  ]
}

2. Trích Xuất Dữ Liệu (Data Extraction)

{
  "role": "Data extraction specialist",
  "task": "Extract structured data from unstructured text",
  "input": "[UNSTRUCTURED TEXT]",
  "output_format": {
    "entities": {
      "people": [{"name": "string", "role": "string"}],
      "organizations": ["string"],
      "locations": ["string"],
      "dates": ["string (ISO 8601 format)"]
    },
    "contact_info": {
      "emails": ["string"],
      "phones": ["string"],
      "websites": ["string"]
    },
    "financial_data": {
      "amounts": [{"value": "number", "currency": "string"}],
      "percentages": ["number"]
    }
  },
  "constraints": [
    "Return null if information not found",
    "Validate email and phone formats",
    "Use ISO country codes for locations"
  ]
}

3. Tóm Tắt Cuộc Họp (Meeting Summary)

{
  "role": "Executive assistant specialized in meeting documentation",
  "task": "Create structured summary from meeting transcript",
  "input_transcript": "[MEETING TRANSCRIPT]",
  "output_format": {
    "meeting_metadata": {
      "date": "string (YYYY-MM-DD)",
      "duration": "number (minutes)",
      "attendees": ["string"],
      "meeting_type": "standup|planning|review|brainstorm"
    },
    "agenda_items": [
      {
        "topic": "string",
        "discussion_summary": "string (max 150 chars)",
        "decisions_made": ["string"],
        "action_items": [
          {
            "task": "string",
            "assigned_to": "string",
            "deadline": "string (YYYY-MM-DD)",
            "priority": "high|medium|low"
          }
        ]
      }
    ],
    "key_decisions": ["string"],
    "follow_up_items": ["string"],
    "next_meeting": {
      "scheduled_date": "string or null",
      "agenda_preview": "string or null"
    }
  }
}

4. Đánh Giá Sản Phẩm (Product Review Analysis)

{
  "role": "E-commerce product review analyst",
  "task": "Analyze customer review and extract insights",
  "input_review": "[CUSTOMER REVIEW TEXT]",
  "output_format": {
    "sentiment_analysis": {
      "overall_sentiment": "positive|neutral|negative",
      "sentiment_score": "number (-1 to 1)",
      "confidence": "number (0-100)"
    },
    "product_aspects": [
      {
        "aspect": "quality|price|shipping|customer_service|features",
        "rating": "number (1-5)",
        "sentiment": "positive|neutral|negative",
        "mentioned_details": ["string"]
      }
    ],
    "pros": ["string (max 5 items)"],
    "cons": ["string (max 5 items)"],
    "purchase_recommendation": "boolean",
    "customer_profile": {
      "experience_level": "first_time|regular|expert",
      "use_case": "string"
    },
    "actionable_feedback": ["string"]
  }
}

5. Tạo Câu Hỏi Quiz (Quiz Generation)

{
  "role": "Educational content creator",
  "task": "Generate quiz questions from provided content",
  "input_content": "[LEARNING MATERIAL]",
  "number_of_questions": 10,
  "output_format": {
    "quiz_metadata": {
      "topic": "string",
      "difficulty": "beginner|intermediate|advanced",
      "estimated_time": "number (minutes)"
    },
    "questions": [
      {
        "question_id": "number",
        "question_text": "string",
        "question_type": "multiple_choice|true_false|fill_blank",
        "options": ["string (4 options for multiple choice)"],
        "correct_answer": "string or number (index)",
        "explanation": "string (why this answer is correct)",
        "difficulty_level": "easy|medium|hard",
        "points": "number"
      }
    ],
    "total_points": "number",
    "passing_score": "number"
  },
  "constraints": [
    "Mix difficulty levels: 40% easy, 40% medium, 20% hard",
    "Each question must test different concept",
    "Explanations should be educational, not just confirming answer"
  ]
}

6. Phân Loại Email (Email Classification)

{
  "role": "Email processing AI assistant",
  "task": "Classify and extract information from email",
  "input_email": "[EMAIL CONTENT]",
  "output_format": {
    "classification": {
      "category": "customer_support|sales|hr|technical|spam|other",
      "priority": "urgent|high|normal|low",
      "requires_response": "boolean",
      "estimated_response_time": "number (hours)"
    },
    "extracted_info": {
      "sender": {
        "name": "string",
        "email": "string",
        "company": "string or null"
      },
      "subject": "string",
      "main_request": "string (one sentence)",
      "action_items": ["string"],
      "mentioned_products": ["string"],
      "attachments": ["string (filenames)"]
    },
    "suggested_department": "string",
    "tags": ["string (max 5)"]
  }
}

7. Tạo Metadata SEO (SEO Metadata Generation)

{
  "role": "SEO specialist",
  "task": "Generate optimized SEO metadata for content",
  "input_content": "[ARTICLE OR PAGE CONTENT]",
  "target_keywords": ["primary keyword", "secondary keywords"],
  "output_format": {
    "seo_title": "string (50-60 characters)",
    "meta_description": "string (150-160 characters)",
    "keywords": ["string (8-12 keywords)"],
    "og_tags": {
      "og_title": "string",
      "og_description": "string",
      "og_type": "article|website|product",
      "suggested_og_image_description": "string"
    },
    "heading_structure": {
      "h1": "string (one main heading)",
      "h2_suggestions": ["string (3-5 subheadings)"]
    },
    "url_slug": "string (kebab-case)",
    "schema_markup_type": "Article|BlogPosting|Product|FAQ",
    "internal_link_suggestions": ["string (related topics)"],
    "readability_score": "number (0-100)"
  }
}

8. Phân Tích Cảm Xúc Khách Hàng (Customer Sentiment)

{
  "role": "Customer experience analyst",
  "task": "Analyze customer feedback sentiment across multiple dimensions",
  "input_feedback": "[CUSTOMER FEEDBACK TEXT]",
  "output_format": {
    "overall_sentiment": {
      "label": "very_positive|positive|neutral|negative|very_negative",
      "score": "number (-1 to 1)",
      "confidence": "number (0-100)"
    },
    "emotion_detection": {
      "primary_emotion": "joy|trust|fear|surprise|sadness|disgust|anger|anticipation",
      "emotion_intensity": "low|medium|high",
      "secondary_emotions": ["string"]
    },
    "aspect_based_sentiment": [
      {
        "aspect": "string",
        "sentiment": "positive|neutral|negative",
        "quotes": ["string (supporting quotes from text)"]
      }
    ],
    "urgency_level": "immediate|high|medium|low",
    "customer_satisfaction_score": "number (1-10)",
    "churn_risk": "high|medium|low|none",
    "recommended_action": "string"
  }
}

9. So Sánh Sản Phẩm (Product Comparison)

{
  "role": "Product comparison analyst",
  "task": "Compare multiple products and provide structured analysis",
  "input_products": ["Product 1 description", "Product 2 description"],
  "output_format": {
    "comparison_summary": "string (max 200 chars)",
    "products": [
      {
        "product_name": "string",
        "category": "string",
        "price_range": "string",
        "key_features": ["string (top 5)"],
        "pros": ["string (max 5)"],
        "cons": ["string (max 5)"],
        "best_for": "string (target user)",
        "overall_rating": "number (1-10)"
      }
    ],
    "feature_matrix": [
      {
        "feature_name": "string",
        "product_1_value": "string",
        "product_2_value": "string",
        "winner": "product_1|product_2|tie"
      }
    ],
    "winner_by_category": {
      "best_value": "string (product name)",
      "best_features": "string",
      "best_quality": "string",
      "best_for_beginners": "string"
    },
    "final_recommendation": "string"
  }
}

10. Tạo User Story (User Story Generator)

{
  "role": "Agile product manager",
  "task": "Convert feature request into structured user stories",
  "input_feature_request": "[FEATURE DESCRIPTION]",
  "output_format": {
    "epic": {
      "title": "string",
      "description": "string",
      "business_value": "string"
    },
    "user_stories": [
      {
        "story_id": "string",
        "title": "string",
        "as_a": "string (user role)",
        "i_want_to": "string (action)",
        "so_that": "string (benefit)",
        "acceptance_criteria": [
          {
            "criteria_id": "number",
            "given": "string (context)",
            "when": "string (action)",
            "then": "string (expected result)"
          }
        ],
        "priority": "must_have|should_have|could_have|wont_have",
        "story_points": "number (1,2,3,5,8,13)",
        "dependencies": ["string (other story IDs)"],
        "technical_notes": "string or null"
      }
    ],
    "total_story_points": "number",
    "estimated_sprints": "number"
  }
}

11. Phân Tích Competitor (Competitor Analysis)

{
  "role": "Market research analyst",
  "task": "Analyze competitor information and create structured insights",
  "input_competitor_data": "[COMPETITOR INFORMATION]",
  "output_format": {
    "competitor_profile": {
      "company_name": "string",
      "industry": "string",
      "market_position": "leader|challenger|follower|nicher",
      "founded_year": "number or null",
      "employee_count_range": "string"
    },
    "swot_analysis": {
      "strengths": ["string (3-5 items)"],
      "weaknesses": ["string (3-5 items)"],
      "opportunities": ["string (3-5 items)"],
      "threats": ["string (3-5 items)"]
    },
    "product_offering": {
      "main_products": ["string"],
      "pricing_strategy": "premium|mid_range|budget|freemium",
      "unique_selling_points": ["string"]
    },
    "marketing_strategy": {
      "main_channels": ["social|email|content|paid_ads|seo"],
      "content_themes": ["string"],
      "brand_voice": "professional|casual|technical|friendly"
    },
    "competitive_advantages": ["string"],
    "vulnerabilities": ["string"],
    "recommended_differentiation": "string"
  }
}

12. Tạo Test Cases (Test Case Generation)

{
  "role": "QA engineer",
  "task": "Generate comprehensive test cases from requirements",
  "input_requirements": "[FEATURE REQUIREMENTS]",
  "output_format": {
    "test_suite": {
      "feature_name": "string",
      "test_type": "functional|integration|regression|performance",
      "priority": "critical|high|medium|low"
    },
    "test_cases": [
      {
        "test_case_id": "string (e.g., TC001)",
        "title": "string",
        "description": "string",
        "preconditions": ["string"],
        "test_steps": [
          {
            "step_number": "number",
            "action": "string",
            "expected_result": "string"
          }
        ],
        "test_data": {
          "input_values": ["string"],
          "expected_output": "string"
        },
        "priority": "critical|high|medium|low",
        "estimated_time": "number (minutes)",
        "dependencies": ["string (other test case IDs)"]
      }
    ],
    "edge_cases": [
      {
        "scenario": "string",
        "expected_behavior": "string"
      }
    ],
    "total_test_cases": "number"
  }
}

13. Tạo Social Media Content Plan

{
  "role": "Social media strategist",
  "task": "Create structured content calendar",
  "input_topic": "[CONTENT TOPIC OR CAMPAIGN]",
  "duration_days": 7,
  "platforms": ["facebook", "instagram", "linkedin", "twitter"],
  "output_format": {
    "campaign_overview": {
      "theme": "string",
      "objectives": ["string"],
      "target_audience": "string",
      "key_messages": ["string (3-5 items)"]
    },
    "content_calendar": [
      {
        "date": "string (YYYY-MM-DD)",
        "platform": "string",
        "content_type": "post|story|reel|carousel|video",
        "caption": "string",
        "hashtags": ["string (max 10)"],
        "call_to_action": "string",
        "visual_description": "string (image/video concept)",
        "posting_time": "string (HH:MM)",
        "expected_engagement": "high|medium|low"
      }
    ],
    "content_themes_distribution": {
      "educational": "number (percentage)",
      "promotional": "number (percentage)",
      "entertaining": "number (percentage)",
      "engagement": "number (percentage)"
    },
    "kpis_to_track": ["string"]
  }
}

14. Code Review Summary

{
  "role": "Senior software engineer",
  "task": "Analyze code and provide structured review",
  "input_code": "[CODE SNIPPET]",
  "programming_language": "javascript",
  "output_format": {
    "code_quality": {
      "overall_score": "number (1-10)",
      "readability": "number (1-10)",
      "maintainability": "number (1-10)",
      "performance": "number (1-10)",
      "security": "number (1-10)"
    },
    "issues_found": [
      {
        "severity": "critical|major|minor|suggestion",
        "category": "bug|security|performance|style|documentation",
        "description": "string",
        "line_number": "number or null",
        "suggested_fix": "string",
        "code_example": "string or null"
      }
    ],
    "best_practices_violations": ["string"],
    "security_concerns": ["string"],
    "performance_issues": ["string"],
    "positive_aspects": ["string"],
    "refactoring_suggestions": [
      {
        "area": "string",
        "current_approach": "string",
        "recommended_approach": "string",
        "benefit": "string"
      }
    ],
    "overall_recommendation": "approve|approve_with_changes|needs_work|reject"
  }
}

15. Chuyển Đổi Dữ Liệu (Data Transformation)

{
  "role": "Data transformation specialist",
  "task": "Convert unstructured data into structured format",
  "input_data": "[RAW DATA OR TEXT]",
  "target_schema": {
    "customers": [
      {
        "id": "number",
        "name": "string",
        "contact": {
          "email": "string",
          "phone": "string"
        },
        "orders": [
          {
            "order_id": "string",
            "date": "string (ISO 8601)",
            "total": "number",
            "items": ["string"]
          }
        ],
        "total_spent": "number",
        "status": "active|inactive"
      }
    ],
    "summary": {
      "total_customers": "number",
      "total_orders": "number",
      "total_revenue": "number",
      "date_range": {
        "from": "string",
        "to": "string"
      }
    }
  },
  "transformation_rules": [
    "Convert all dates to ISO 8601 format",
    "Calculate total_spent from all orders",
    "Mark customer inactive if no orders in 90 days",
    "Remove duplicate entries",
    "Validate email format"
  ]
}

Kỹ Thuật Nâng Cao

Xử Lý Nested JSON và Arrays

Thách thức: JSON lồng nhau phức tạp thường khiến AI trả về structure không đúng.

Giải pháp:

{
  "task": "Parse complex nested data",
  "output_format": {
    "company": {
      "name": "string",
      "departments": [
        {
          "dept_id": "string",
          "dept_name": "string",
          "employees": [
            {
              "emp_id": "number",
              "name": "string",
              "position": "string",
              "skills": ["string"],
              "projects": [
                {
                  "project_id": "string",
                  "role": "string",
                  "hours_allocated": "number"
                }
              ]
            }
          ],
          "total_employees": "number"
        }
      ]
    }
  },
  "instructions": [
    "IMPORTANT: Maintain exact nested structure as shown",
    "For arrays, ensure consistent object structure across all items",
    "Calculate total_employees by counting employees array length",
    "Return empty arrays [] if no data, never use null for arrays"
  ]
}

💡 Pro Tip: Khi làm việc với nested JSON, cung cấp example hoàn chỉnh với đầy đủ levels of nesting. Điều này giảm 90% lỗi cấu trúc.

Conditional Fields và Dynamic Schema

Use case: Output schema thay đổi dựa trên input type

{
  "task": "Product information extraction",
  "output_format": {
    "product_type": "physical|digital|service",
    "common_fields": {
      "name": "string",
      "price": "number",
      "description": "string"
    },
    "conditional_fields": {
      "if_physical": {
        "weight": "number",
        "dimensions": {"length": "number", "width": "number", "height": "number"},
        "shipping_class": "string"
      },
      "if_digital": {
        "file_format": "string",
        "file_size_mb": "number",
        "download_link": "string"
      },
      "if_service": {
        "duration_hours": "number",
        "service_location": "onsite|remote|hybrid",
        "availability": ["string (days of week)"]
      }
    }
  },
  "instructions": [
    "First determine product_type from input",
    "Include common_fields for ALL products",
    "Include ONLY relevant conditional_fields based on product_type",
    "Use null for inapplicable fields"
  ]
}

Handling Large Datasets với Pagination

Vấn đề: AI có giới hạn output length

Giải pháp:

{
  "task": "Process large dataset with pagination",
  "pagination": {
    "current_page": 1,
    "items_per_page": 20,
    "total_items_estimate": "number or null"
  },
  "output_format": {
    "metadata": {
      "page": "number",
      "per_page": "number",
      "total_items_processed": "number",
      "has_more": "boolean"
    },
    "items": [
      {
        "item_id": "string",
        "data": "object"
      }
    ],
    "next_page_instruction": "string or null"
  },
  "instructions": [
    "Process ONLY {items_per_page} items",
    "If more items exist, set has_more: true",
    "Provide clear next_page_instruction for continuation"
  ]
}

Error Handling và Validation

Best practice: Luôn bao gồm error handling trong schema

{
  "output_format": {
    "success": "boolean",
    "data": {
      "...": "your normal schema here"
    },
    "errors": [
      {
        "error_code": "string",
        "error_message": "string",
        "field": "string or null",
        "severity": "critical|warning|info"
      }
    ],
    "validation_results": {
      "passed": "boolean",
      "failed_rules": ["string"],
      "warnings": ["string"]
    },
    "metadata": {
      "processed_at": "string (ISO 8601)",
      "processing_time_ms": "number",
      "version": "string"
    }
  },
  "error_handling_rules": [
    "If critical error occurs, set success: false",
    "Always populate errors array, use [] if no errors",
    "Include field name for field-specific errors",
    "Provide actionable error messages"
  ]
}

Best Practices Từ Chuyên Gia

✅ DO (Nên Làm)

  1. Luôn cung cấp example output cụ thể

    • Tăng accuracy lên 80%
    • Giảm số lần revise prompt
  2. Chỉ định rõ data types

    "age": "number (integer, 0-120)",
    "email": "string (valid email format)",
    "tags": "array of strings (max 10 items)"
    
  3. Sử dụng validation rules

    • Đảm bảo data quality
    • Giảm post-processing effort
  4. Chia nhỏ complex tasks

    • Thay vì 1 prompt lớn, chia thành 2-3 prompts nhỏ
    • Mỗi prompt xử lý 1 aspect cụ thể
  5. Test với edge cases

    • Empty inputs
    • Very long inputs
    • Special characters
    • Multiple languages

❌ DON'T (Tránh Làm)

  1. ❌ Không chỉ định data type

    // BAD
    "price": "the price of product"
    
    // GOOD
    "price": "number (with 2 decimal places)"
    
  2. ❌ Không validate output

    • Luôn kiểm tra AI có trả về đúng JSON syntax
    • Validate data types trước khi sử dụng
  3. ❌ Schema quá phức tạp trong 1 prompt

    • Giới hạn max 3 levels of nesting
    • Chia thành multiple prompts nếu cần
  4. ❌ Thiếu fallback values

    // BAD
    "optional_field": "string"
    
    // GOOD
    "optional_field": "string or null"
    
  5. ❌ Không xử lý errors

    • Luôn include error handling trong schema
    • Cung cấp fallback mechanism

Performance Optimization Tips

  1. Giảm prompt size:

    • Loại bỏ instructions không cần thiết
    • Dùng abbreviations cho field names trong internal processing
  2. Caching strategies:

    • Lưu template prompts thường dùng
    • Reuse successful prompt patterns
  3. Batch processing:

    {
      "task": "Process multiple items in batch",
      "batch_size": 10,
      "items": ["item1", "item2", "..."],
      "output_format": {
        "results": [
          {"item_id": "string", "result": "object"}
        ]
      }
    }
    
  4. Token optimization:

    • Sử dụng concise field names
    • Loại bỏ redundant instructions
    • Compress example outputs khi có thể

Công Cụ và Tài Nguyên

🔧 Công Cụ Thiết Yếu:

  1. JSONLint - Miễn phí

    • Validate JSON syntax real-time
    • Beautify và format JSON
    • Best for: Quick validation khi viết prompts
  2. JSON Schema Validator - Miễn phí

    • Validate JSON against schema
    • Generate schema from example
    • Best for: Complex schema validation
  3. Postman - Freemium

    • Test API responses
    • Save và organize JSON prompts
    • Best for: Integration testing với AI APIs
  4. Visual Studio Code với JSON extension - Miễn phí

    • Auto-completion cho JSON
    • Real-time syntax validation
    • Schema validation support
    • Best for: Writing và managing large prompt libraries

📚 Tài Nguyên Học Tập:

📊 Templates Miễn Phí:

  • Prompt Template Library: 50+ JSON prompt templates cho use cases phổ biến
  • Schema Generator Tool: Tự động generate JSON schema từ mô tả text
  • Validation Checklist: Checklist 20 điểm để validate JSON prompts

Xử Lý Lỗi Thường Gặp

Lỗi 1: AI Trả Về Text Thay Vì JSON

Vấn đề:

The analysis shows positive sentiment with...
(Text instead of JSON)

Nguyên nhân:

  • Instruction không đủ rõ ràng
  • Model không ở JSON mode

Giải pháp:

{
  "CRITICAL_INSTRUCTION": "You MUST return ONLY valid JSON. Do not include any text before or after the JSON object.",
  "response_format": "json_object",
  "output_format": {
    "...": "your schema"
  },
  "example_output": {
    "...": "concrete example"
  }
}

Thêm vào prompt:

"IMPORTANT: Your response must be ONLY a valid JSON object. Start with { and end with }. No additional text."

Lỗi 2: Missing hoặc Null Fields

Vấn đề:

{
  "name": "Product A",
  "price": null,  // Should have value
  "description": null
}

Giải pháp:

{
  "required_fields": [
    "name",
    "price",
    "description"
  ],
  "instructions": [
    "ALL fields in required_fields array MUST have non-null values",
    "If information is not available in input, use reasonable defaults:",
    "- For price: use 0 or 'price_not_available'",
    "- For description: use 'No description provided'",
    "- For dates: use current date or null with explanation in notes field"
  ],
  "output_format": {
    "name": "string (required)",
    "price": "number or string (required, never null)",
    "description": "string (required, min 10 chars)",
    "notes": "string or null (for metadata/explanations)"
  }
}

Lỗi 3: Inconsistent Data Types

Vấn đề:

{
  "rating": "4.5",  // Should be number
  "count": 100,     // Correct
  "active": "true"  // Should be boolean
}

Giải pháp:

{
  "strict_typing": true,
  "output_format": {
    "rating": "number (float with 1 decimal place, example: 4.5)",
    "count": "number (integer, example: 100)",
    "active": "boolean (true or false, NOT string)"
  },
  "type_enforcement_rules": [
    "Numbers must be numeric type, not strings",
    "Booleans must be true/false, not 'true'/'false' strings",
    "Null must be JSON null, not string 'null'",
    "Arrays must use [], not strings"
  ],
  "validation_example": {
    "rating": 4.5,
    "count": 100,
    "active": true
  }
}

Lỗi 4: Array Structure Không Nhất Quán

Vấn đề:

{
  "items": [
    {"name": "Item 1", "price": 100},
    {"name": "Item 2"},  // Missing price
    {"title": "Item 3", "price": 150}  // Different field name
  ]
}

Giải pháp:

{
  "output_format": {
    "items": [
      {
        "name": "string (required for ALL items)",
        "price": "number (required for ALL items, use 0 if unknown)"
      }
    ]
  },
  "array_consistency_rules": [
    "ALL objects in an array MUST have identical structure",
    "ALL required fields must be present in every array item",
    "Field names must be consistent across all items",
    "If a field is optional, use null consistently for missing values"
  ],
  "correct_example": {
    "items": [
      {"name": "Item 1", "price": 100},
      {"name": "Item 2", "price": 0},
      {"name": "Item 3", "price": 150}
    ]
  }
}

Lỗi 5: Nested JSON Depth Issues

Vấn đề: AI không maintain nested structure correctly ở level 3+

Giải pháp:

{
  "max_nesting_depth": 3,
  "strategy": "break_into_flat_structure_if_needed",
  "output_format": {
    "company": {
      "name": "string",
      "departments": [
        {
          "id": "string",
          "name": "string",
          "manager_id": "string (reference to separate managers array)"
        }
      ],
      "managers": [
        {
          "id": "string",
          "name": "string",
          "department_id": "string"
        }
      ]
    }
  },
  "instructions": [
    "Instead of deeply nested objects, use flat arrays with reference IDs",
    "Max 3 levels of nesting",
    "Use IDs to link related data across arrays"
  ]
}

FAQ

Q: JSON prompt có hoạt động với tất cả các AI models không?

A: Có, nhưng với mức độ hỗ trợ khác nhau:

  • ChatGPT (GPT-4, GPT-3.5): Hỗ trợ tốt, có JSON mode chuyên biệt
  • Claude (Anthropic): Hỗ trợ tốt, đặc biệt mạnh với complex nested JSON
  • Gemini (Google): Hỗ trợ tốt từ phiên bản Pro trở lên
  • Local models (LLaMA, Mistral): Hỗ trợ tùy theo model size và fine-tuning

Lưu ý: Với GPT-4 và GPT-3.5-turbo, sử dụng parameter response_format: { "type": "json_object" } để bật JSON mode, đảm bảo output luôn là valid JSON.

Q: Làm sao biết JSON prompt của tôi đã tối ưu chưa?

A: Kiểm tra 5 tiêu chí này:

  1. Consistency (90%+): Chạy cùng prompt 10 lần, nhận được cùng structure ≥ 9 lần
  2. Completeness (100%): Không có null values cho required fields
  3. Type accuracy (100%): Tất cả fields đúng data type
  4. Performance: AI response time < 5 giây cho prompt đơn giản
  5. Validation: Pass JSON schema validator không có errors

Tool để test: Tạo test suite với 5-10 sample inputs và verify outputs.

Q: Khi nào nên dùng nested JSON vs flat structure?

A:

Dùng Nested JSON khi:

  • Data có quan hệ cha-con tự nhiên (company → departments → employees)
  • Cần maintain context hierarchy
  • Output dùng cho frontend display trực tiếp
  • Nesting depth ≤ 3 levels

Dùng Flat Structure khi:

  • Nesting depth > 3 levels
  • Cần query/filter data dễ dàng
  • Lưu vào relational database
  • Performance là ưu tiên (flat structure process nhanh hơn)

Example:

// Nested (good for depth ≤ 3)
{
  "company": {
    "departments": [
      {"id": 1, "employees": [...]}
    ]
  }
}

// Flat (good for depth > 3 or database storage)
{
  "companies": [...],
  "departments": [...],
  "employees": [...]
}

Q: AI trả về JSON nhưng thêm markdown code blocks, làm sao xử lý?

A: Đây là vấn đề phổ biến. AI thường trả về:

```json
{
  "data": "value"
}

**Giải pháp:**

1. **Trong prompt, yêu cầu rõ ràng:**
```json
{
  "CRITICAL": "Return ONLY the JSON object. No markdown, no code blocks, no explanations.",
  "format": "Start directly with { and end with }",
  "output_format": {...}
}
  1. Post-processing code (nếu vẫn gặp):
function extractJSON(response) {
  // Remove markdown code blocks
  let cleaned = response.replace(/```json\n?/g, '').replace(/```\n?/g, '');
  // Trim whitespace
  cleaned = cleaned.trim();
  // Parse JSON
  return JSON.parse(cleaned);
}

Q: Chi phí sử dụng JSON prompt có cao hơn text prompt không?

A: Có, nhưng không đáng kể:

Token count comparison:

  • Simple text prompt: ~50-100 tokens
  • JSON prompt (với schema): ~150-300 tokens
  • Increase: ~2-3x input tokens

Tuy nhiên:

  • ✅ Giảm 70% thời gian post-processing (tiết kiệm developer time)
  • ✅ Không cần multiple API calls để parse/fix format
  • ✅ Ít lỗi hơn = ít retry calls hơn

ROI: JSON prompt có ROI cao hơn do giảm total cost of ownership.

Q: JSON prompt có phù hợp cho tiếng Việt không?

A: Hoàn toàn có, và rất hiệu quả!

Best practices cho tiếng Việt:

{
  "language": "vietnamese",
  "encoding": "UTF-8",
  "instructions": [
    "Trả về tiếng Việt có dấu đầy đủ",
    "Sử dụng vocabulary phù hợp với ngữ cảnh",
    "Giữ nguyên tone formal/informal theo yêu cầu"
  ],
  "output_format": {
    "tieu_de": "string (tiếng Việt có dấu)",
    "mo_ta": "string",
    "tu_khoa": ["string (tiếng Việt)"]
  },
  "example_output": {
    "tieu_de": "Hướng Dẫn Viết Prompt AI",
    "mo_ta": "Bài viết chi tiết về cách viết prompt hiệu quả",
    "tu_khoa": ["AI", "Prompt Engineering", "Hướng dẫn"]
  }
}

Lưu ý:

  • Dùng field names bằng tiếng Anh (snake_case) để dễ process
  • Content values dùng tiếng Việt có dấu
  • Test kỹ với Vietnamese special characters

Kết Luận và Bước Tiếp Theo

Tóm Tắt Các Điểm Chính:

  • 🎯 JSON Prompt giúp cấu trúc hóa output AI: Tăng accuracy 85%, giảm 70% thời gian xử lý dữ liệu
  • 🎯 Luôn bao gồm 5 elements: Role, Task, Output Format, Example, Constraints
  • 🎯 Test và validate là bắt buộc: Không bao giờ deploy prompt chưa test với edge cases
  • 🎯 Sử dụng templates có sẵn: 15+ mẫu trong bài này cover 90% use cases phổ biến
  • 🎯 Optimize liên tục: Monitor performance metrics và adjust prompts based on data

Hành Động Ngay Hôm Nay:

  1. Ngay bây giờ:

    • Chọn 1 trong 15 templates phù hợp với use case của bạn
    • Customize với data thực tế của bạn
    • Chạy test với 3-5 sample inputs
  2. Tuần này:

    • Build prompt library với 5-10 prompts thường dùng
    • Setup validation workflow (JSONLint + testing)
    • Document successful patterns
  3. Tháng này:

    • Triển khai JSON prompts vào production
    • Monitor metrics: accuracy, processing time, error rate
    • A/B test different prompt variations
    • Build internal best practices document

Tiếp Tục Học Hỏi:

Khám phá các bài viết liên quan:

Chia Sẻ Kinh Nghiệm

Bạn đã thử JSON prompts chưa? Chia sẻ kết quả hoặc thắc mắc của bạn tại contact@anhhochieu.vn - chúng tôi sẽ tư vấn miễn phí và có thể feature case study của bạn trong bài viết tiếp theo!


Cập nhật: Bài viết được cập nhật lần cuối vào 01/11/2025 với 15 mẫu JSON prompts mới nhất và best practices từ OpenAI, Anthropic documentation.

Tài nguyên bổ sung: Download JSON Prompt Template Pack (Miễn phí) - Bao gồm 50+ templates và validation checklist.