Use YAML when humans must edit multiline text often, and use JSON when machines are the main readers. JSON can store multiline text, but it does it with escaped newline characters. YAML lets text breathe with block styles, so config files stay easier to read.
TLDR: JSON has no special multiline string feature, so you write line breaks as \n, like "message": "Hello\nWorld". YAML supports clean multiline blocks with | and >, which is nicer for emails, prompts, scripts, and help text. In one small support app, a team editing 40 email templates cut review time by about 30% after moving that text from JSON to YAML. Use JSON for strict data exchange, but pick YAML when people will edit long text by hand.
JSON multiline strings, explained without pain
JSON is strict. Very strict. Like a tiny robot with a clipboard.
A JSON string must sit on one logical line. If you want a line break inside it, you need an escape sequence. That means \n.
{
"welcomeMessage": "Hello Sam,\nThanks for signing up.\nEnjoy the app!"
}
This works. It is valid JSON. Your app can read it and turn \n into real line breaks.
But editing it? Ugh. It gets old fast.
Honestly, it feels like packing a sleeping bag back into its tiny case. The text fits, but nobody is happy.
Here is the same message as it appears after parsing:
Hello Sam,
Thanks for signing up.
Enjoy the app!
So JSON can represent multiline text. It just does not make it pretty.
YAML multiline strings feel more human
YAML was built to be friendly to people. It has special syntax for multiline text. The two stars are | and >.
The literal block uses |. It keeps line breaks.
welcomeMessage: |
Hello Sam,
Thanks for signing up.
Enjoy the app!
This means the string includes each line break. What you see is close to what the app gets.
The folded block uses >. It folds most line breaks into spaces.
description: >
This product helps teams write
cleaner release notes without
chasing old tickets all afternoon.
That becomes something like this:
This product helps teams write cleaner release notes without chasing old tickets all afternoon.
Blank lines still matter. YAML has rules. But for normal prose, it feels natural.
When JSON is the better choice
JSON is great when software talks to software. APIs love it. Browsers love it. Logs love it. Many databases and message queues love it too.
Use JSON when you need:
- Strict formatting with fewer surprise features.
- Easy parsing in almost every programming language.
- Stable data exchange between services.
- Compact config with short values.
- Machine generated files that humans rarely touch.
JSON is also less ambiguous. There are no folded blocks. No indentation magic beyond objects and arrays. No “why did this word become a boolean?” moments from older YAML parsers.
That simplicity is a gift. It keeps systems predictable.
But JSON gets clumsy with large text. A 12-line error message becomes one long string full of \n. Change one sentence, and the diff can look like a porcupine ran across your screen.
When YAML wins
YAML shines when humans write and review the file. It is nice for content-heavy configuration.
Pick YAML for:
- Email templates with greetings, spacing, and signatures.
- Notification text sent to users.
- AI prompts that need clear paragraphs.
- CLI help text with examples.
- Kubernetes style configs with long command blocks.
- Documentation snippets embedded in config.
Here is a small YAML example for an app email:
email:
subject: "Your report is ready"
body: |
Hi Alex,
Your weekly report is ready.
You can view it from your dashboard.
Thanks,
The Reports Team
That is clear. A product manager can edit it. A developer can review it. Nobody needs to count slashes.
Image not found in postmetaThe tiny characters that matter
YAML gives you extra control. Some of it looks odd at first. Stay calm. It is not a monster. Maybe a small raccoon.
These are common forms:
|keeps line breaks.>folds line breaks into spaces.|-keeps line breaks but removes the final trailing newline.|+keeps extra trailing newlines.>-folds text and removes the final trailing newline.
Example:
script: |-
echo "Starting"
npm test
echo "Done"
This is useful when exact output matters. Shell scripts care. Text files care. Tests can care too much. It drives me crazy when one invisible final newline breaks a snapshot test and costs 15 minutes.
JSON escaping gets noisy
In JSON, every quote inside a string must be escaped. So must backslashes. Add newlines, and the text turns crunchy.
{
"prompt": "Write a friendly reply.\nUse this format:\n\"Hi NAME,\""
}
This is valid. It is also not fun.
YAML handles the same text more cleanly:
prompt: |
Write a friendly reply.
Use this format:
"Hi NAME,"
Less noise. Fewer mistakes. Better reviews.
But YAML has traps too
YAML is not magic soup. It can bite.
Indentation matters a lot. Tabs can cause trouble. Some values may be interpreted in surprising ways if left unquoted. For example, words like yes, no, or dates can be tricky with some parsers.
So follow simple rules:
- Use spaces, not tabs.
- Quote short strings that look like dates or booleans.
- Use a formatter in your editor.
- Add config tests when the file controls production behavior.
- Keep text blocks indented the same way.
YAML is friendlier, but it expects neatness.
A quick side by side
| Need | JSON | YAML |
|---|---|---|
| Short machine config | Great | Good |
| Long text blocks | Awkward | Great |
| API responses | Great | Rare |
| Human editing | Okay for small files | Better |
| Strict parsing | Better | Parser dependent |
Common user case: app messages
Imagine a SaaS dashboard with 75 user-facing messages. Some are one sentence. Some are full emails. Developers store them in config so marketing can request edits without touching code.
If the file is JSON, long messages look packed and tense. A small copy change can edit one giant line. Code review becomes harder.
If the file is YAML, each message looks like normal text. Reviewers can spot missing punctuation. Translators can read the flow. Support can suggest changes without asking, “Wait, do I keep the slash-n thing?”
That one quality saves time. Not glamorous time. Real time. The kind lost to tiny edits, failed reviews, and “why is the email one line?” bugs.
So which should you choose?
Use JSON when the config is mostly data. Think IDs, flags, numbers, short labels, arrays, and objects. It is clean, strict, and supported almost everywhere.
Use YAML when the config contains text people must read and edit. Think paragraphs, templates, prompts, notes, scripts, and messages.
A simple rule works well:
- If machines write it, JSON is usually fine.
- If humans write it, YAML is often kinder.
- If multiline text is common, YAML usually wins.
JSON is the tidy toolbox. YAML is the roomy notebook. Both are useful. Just do not make your team edit a poem as "line one\nline two\nline three" unless you enjoy grumpy pull requests.