YAML to JSON: keep the line breaks you meant
A pipe, a greater-than sign and a minus sign produce different strings. Five small examples show why.
A YAML paragraph can look like two lines in your editor but become one line in JSON. The character after the key decides how those line breaks are read; a minus sign decides whether the final newline stays.
Open the CSV, JSON & YAML converter, paste this example, choose YAML in From and JSON in To:
literal: |
Ready to share.
Check the link.
folded: >
Ready to share.
Check the link.
literal_strip: |-
Ready to share.
Check the link.
folded_strip: >-
Ready to share.
Check the link.
Read the output before changing the text
The converter produces these four strings:
{
"literal": "Ready to share.\nCheck the link.\n",
"folded": "Ready to share. Check the link.\n",
"literal_strip": "Ready to share.\nCheck the link.",
"folded_strip": "Ready to share. Check the link."
}
In JSON source, \n represents a newline inside the string. It is not an extra backslash that your reader will see after parsing the JSON. Indentation outside the quotes only makes the JSON easier to read; it does not change that string.
For a two-line caption, the third version is often the useful one. For one sentence wrapped across two lines in a configuration file, the fourth keeps it together without a final newline.
Choose the block style and the ending separately
| YAML marker | Between ordinary text lines | At the end of this sample |
|---|---|---|
| |
Keeps a newline | Keeps one final newline |
> |
Folds the break into a space | Keeps one final newline |
|- |
Keeps a newline | Removes the final newline |
>- |
Folds the break into a space | Removes the final newline |
These rules describe non-empty blocks like this sample. YAML calls the ending rule “chomping”; the block scalar specification also defines a plus sign for retaining trailing empty lines.
Folding does not erase every line break. Blank lines and more-indented lines have separate rules. Use a small test before converting a message, prompt or shell command whose spacing matters.
A blank line still separates paragraphs
This example uses folded text but includes a blank line:
paragraphs: >-
First paragraph.
Second paragraph.
Its converted value is:
{
"paragraphs": "First paragraph.\nSecond paragraph."
}
There is one newline between the two paragraphs, not a space and not two newlines. If you need exact blank lines, use a literal block and check the resulting string.
Indent the block's content with spaces under its key. Moving a line left can end the block or make the document invalid. A broken document should be corrected at the source rather than “fixed” by removing all whitespace.
Keep the YAML if comments matter
Download the complete YAML sample and its converted JSON. The sample includes all five cases above.
Conversion preserves the interpreted values, not YAML's original presentation. Comments, block-style markers and alias notation are not preserved in JSON. Keep the original YAML if you will edit the configuration again; converting it back will not restore those details.
On 12 October 2026, we ran the downloadable file through the tool's conversion function and checked every string, including its final newline. The results file records the expected values and download hashes. The tool converts locally in your browser, but you should still use a sanitised sample when sharing a configuration with someone else.