aillmprompt-engineeringjson

JSON vs Markdown vs XML in LLM Prompts: What Works Best

·8 min read·AI & JSON

Format is part of the prompt

How you structure the context you give a model — not just its content — affects accuracy, token cost, and how reliably the model separates instructions from data. Three formats dominate: JSON, Markdown, and XML-style tags. Each has a sweet spot.

When to use each

FormatBest forWeakness
JSONPassing structured records, tool results, examples the model should treat as dataPunctuation-heavy; verbose for prose
MarkdownHuman-readable instructions, headings, lists, documentsAmbiguous boundaries between sections
XML-style tagsClearly delimiting sections and untrusted inputSlightly more tokens than Markdown

JSON for data, not instructions

JSON is ideal when you are handing the model records it should reason over — API responses, database rows, a list of examples. It signals "this is structured data," and the model handles it well:

text
Here are the user's recent orders:
[{"id":"A1","total":42,"status":"shipped"},{"id":"A2","total":9,"status":"pending"}]

Which orders still need to ship?

Minify this JSON to save tokens — the model does not need indentation.

XML tags for delimiting

Many model providers note that XML-style tags are an effective way to separate parts of a prompt and to fence off untrusted input. The boundaries are unambiguous:

text
<instructions>
Summarize the document in three bullet points.
</instructions>

<document>
{user-supplied text here}
</document>

Because <document> clearly marks data, the model is less likely to follow instructions hidden inside it — a simple defense against prompt injection.

Markdown for instructions

For the instruction portion of a prompt — what you want done, in what style, with what constraints — Markdown is the most natural and readable. Headings and lists map well to how models were trained on human text:

text
## Task
Rewrite the paragraph for a beginner audience.

## Rules
- Keep it under 100 words
- Avoid jargon

A practical hybrid

Real prompts mix all three: Markdown for instructions, XML tags to fence sections, and JSON for the structured data inside a section.

text
## Task
Classify each review's sentiment.

<reviews>
[{"id":1,"text":"Fast and reliable"},{"id":2,"text":"Broke in a week"}]
</reviews>

Respond with JSON: [{"id":number,"sentiment":"positive"|"negative"}]

Tips

  • Match output format to consumer. If code consumes the output, demand JSON. If a human reads it, Markdown is friendlier.
  • Be consistent. Pick one delimiter style and stick to it across a prompt.
  • Fence untrusted input with tags, always.
  • Minify data JSON, keep instruction Markdown readable.

Frequently asked questions

Use JSON when a program parses the result; use Markdown when a person reads it. Never ask for Markdown-fenced JSON if code will parse it.

They help by clearly marking which content is data versus instructions, but they are not a complete defense — always validate output and treat user text as untrusted.

Yes, modestly. JSON and XML add punctuation tokens; Markdown is leaner for prose. For large data blocks, minified JSON is efficient.

Try JSON Minifier

Minify the JSON you inject into prompts to save tokens.