Writing AI Prompts as Code: JSON and XML for Structured Precision

This post explores how to craft AI prompts using structured formats like JSON and XML for enhanced precision and repeatability.

Artificial Intelligence (AI) can do wonderful things when you ask the right questions, but crafting those questions—known as prompts—can sometimes feel more like art than science. However, when we look at AI prompts from a structured perspective, like programming, things start to become clearer, especially when using structured formats like JSON or XML.

Using formats like these allows AI interactions to be more repeatable, predictable, and shareable. Today, we’ll dive into writing AI prompts as code, specifically in JSON and XML, and explore how this structured approach can turn vague intentions into precision instruments.

Why Write AI Prompts as Code?

Writing prompts as code is useful when you’re dealing with complex tasks. It allows you to specify:

  • Roles and personas for the AI (e.g., making it an expert in a specific domain)
  • Specific tasks for the AI to perform
  • Step-by-step instructions to help the AI break down a problem
  • Guidelines and criteria that ensure the AI adheres to specific standards
  • Output formats for precise and machine-readable results

In essence, it’s a way to communicate not just what you want the AI to do, but how to think about the problem—step by step.

Let’s look at an example in JSON.

Example of a JSON Prompt

Below is an example of a JSON prompt designed to classify whether a business operates as an e-commerce entity based on its description:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
  "task": "Classify a business as e-commerce or not based on its description.",
  "persona": "You are an expert in e-commerce business models.",
  "steps": [
    "1. Review the business description.",
    "2. Apply criteria for e-commerce classification.",
    "3. Make a decision with reasoning.",
    "4. Return result as JSON."
  ],
  "guidelines": [
    {
      "criterion": "Core Business Model",
      "description": "Does the business sell products/services online?"
    },
    {
      "criterion": "Primary Sales Channel",
      "description": "Is the primary sales channel digital/online?"
    }
  ],
  "output_examples": [
    {
      "input": "Amazon",
      "chain_of_thought": "Amazon operates primarily online, making it e-commerce.",
      "output": "True"
    },
    {
      "input": "Goldman Sachs",
      "chain_of_thought": "Goldman Sachs offers financial services, not products for sale online.",
      "output": "False"
    }
  ]
}

Key Elements in the JSON Structure

Let’s break this JSON prompt down:

  • Task: Defines what the AI needs to do. Here, the task is simple: classify whether a business is e-commerce or not.
  • Persona: By defining a persona, you help the AI adopt a specific mindset or expertise. In this case, we tell it that it’s an expert in e-commerce models.
  • Steps: These are specific actions the AI should follow. You can think of this as “instructions” for solving the task. Step-by-step breakdowns help AI process the problem methodically.
  • Guidelines: This section provides specific criteria that the AI should use to make decisions. Here, we’re checking the business model and the primary sales channel.
  • Output Examples: These help guide the AI by providing examples of inputs and the correct outputs, along with reasoning (a “chain of thought”). This is particularly helpful when the task involves nuanced judgment.

Why JSON?

JSON is great for writing AI prompts as code because it’s:

  • Human-readable: Easy for developers (or even non-developers) to understand at a glance.
  • Machine-parsable: A natural fit for APIs and AI systems that expect structured input.
  • Hierarchical: Perfect for representing complex information like steps, criteria, and examples.

Writing AI Prompts in XML

While JSON might be a more popular choice for modern applications, XML (Extensible Markup Language) has its strengths—especially when there’s a need for more explicit structuring or if the environment prefers XML for data interchange. Here’s how the same prompt could be written in XML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<prompt>
  <task>Classify a business as e-commerce or not based on its description.</task>
  <persona>You are an expert in e-commerce business models.</persona>
  <steps>
    <step>1. Review the business description.</step>
    <step>2. Apply criteria for e-commerce classification.</step>
    <step>3. Make a decision with reasoning.</step>
    <step>4. Return result as XML.</step>
  </steps>
  <guidelines>
    <criterion>
      <name>Core Business Model</name>
      <description>Does the business sell products/services online?</description>
    </criterion>
    <criterion>
      <name>Primary Sales Channel</name>
      <description>Is the primary sales channel digital/online?</description>
    </criterion>
  </guidelines>
  <outputExamples>
    <example>
      <input>Amazon</input>
      <chainOfThought>Amazon operates primarily online, making it e-commerce.</chainOfThought>
      <output>True</output>
    </example>
    <example>
      <input>Goldman Sachs</input>
      <chainOfThought>Goldman Sachs offers financial services, not products for sale online.</chainOfThought>
      <output>False</output>
    </example>
  </outputExamples>
</prompt>

Key Differences in XML

  • Tags: XML uses explicit opening and closing tags to define elements, making it more verbose than JSON. This can be useful in systems where clarity is prioritized over conciseness.
  • Nesting: XML’s structure is more rigidly defined with opening and closing tags, which can make hierarchical relationships between data more explicit.

JSON vs. XML: Which to Use?

  • When to Use JSON: If your system already works with JSON (e.g., web APIs), or if you want a more concise and readable format, JSON is often the better choice. It’s lightweight and easier to work with in JavaScript-heavy environments.
  • When to Use XML: If you’re working in a legacy environment, or a system that already relies heavily on XML (e.g., many enterprise solutions), XML may be a better fit. XML is also more self-descriptive, which can be useful when handling complex hierarchies.

Best Practices for Writing AI Prompts in Code

Now that we’ve seen examples in both formats, here are some general tips for writing effective AI prompts in structured formats like JSON and XML:

1. Be Explicit and Clear

AI may be smart, but it isn’t clairvoyant. Your prompts should clearly define what you want, step by step. Don’t assume the AI “understands” context unless you’ve explicitly provided it.

2. Use a Step-by-Step Approach

Break down complex tasks into smaller, digestible steps. Not only does this help the AI, but it also makes it easier to debug your prompt when things go wrong. Think of each step as a “mini-task” the AI needs to complete.

3. Provide Detailed Criteria

If you’re expecting judgment-based decisions, give the AI specific criteria to evaluate. In the e-commerce example, we used “Core Business Model” and “Primary Sales Channel” as guidelines. You could even expand on those with weights or thresholds if needed.

4. Offer Examples (And Reasoning!)

Including examples of inputs, outputs, and the reasoning behind the decision (“chain of thought”) is crucial. This gives the AI a template to follow and reduces ambiguity.

5. Choose Your Output Format Wisely

Specify exactly how you want the AI to respond. JSON, XML, plain text, or something else? Be clear about the desired output so that it can be parsed or interpreted correctly by your system.

Real-World Use Cases

Structured AI prompts are invaluable in many industries, from automating data entry to making complex business decisions. For example:

  • Lead Generation: Tools like TypeCharm can enrich business data automatically. By setting up AI with structured JSON prompts, you can retrieve detailed insights about businesses, such as whether they fit into specific sales categories like B2B or B2C.
  • Customer Support: AI-driven chatbots can handle customer queries based on structured prompts that guide the conversation, using JSON or XML to decide how to proceed at each step.
  • Data Validation: Automating validation processes becomes easier when you use structured prompts to dictate how AI should assess data quality.

The Future of AI Prompting: Beyond JSON and XML?

As AI continues to evolve, we might start seeing hybrid formats or even entirely new structures optimized for prompt engineering. What’s certain is that structured prompts, like those in JSON or XML, will remain a foundation for anyone looking to harness the power of AI effectively.

Whether you’re running a business automation workflow or refining the output of your cold outreach campaigns, tools like TypeCharm can help you build smarter, more scalable processes by integrating AI’s ability to handle structured inputs efficiently. Structured prompting is here to stay—are you ready to master it?


Conclusion

Writing AI prompts as code, especially in JSON and XML formats, opens up a whole new level of control over how AI understands and solves problems. It’s not just about the task you want to complete but also about guiding the AI on how to think through that task, step by step, with crystal-clear criteria and examples.

These techniques are essential for anyone working with AI, from developers to marketers. If you’re ready to dive deeper, start experimenting with your

own structured prompts, and you’ll soon discover that AI doesn’t just need questions—it needs instructions.