Distinguishing a JSON string’s newline escape from a literal line break
Inside a JSON string, a line break has to be written as the two characters backslash and n. A real line break between the quotation marks makes the document invalid, and this worksheet shows both inputs side by side.
What the specification requires
RFC 8259, the JSON specification, says that characters in a string from U+0000 through U+001F, the control characters, must be escaped. A newline is U+000A, one of them. A writer who wants a line break inside a string value therefore types a backslash followed by the letter n. A raw newline between the quotation marks is a control character that has not been escaped, and a strict parser refuses the document. Layout newlines between properties, outside any string, are ordinary whitespace and are allowed.
The valid control
The first input is {"note":"one\ntwo"} as the formatter receives it, a single line of text, with the characters backslash and n between one and two. The parser reads that pair as the escape for a line feed, so the decoded string contains a line break, while the source text stays on a single line. The compact output is the same text, and the valid result is true. Whatever display shows the JSON will show the backslash and n, and the application using the data will see a two-line note after decoding.
The rejected control
The second input has a real line break between one and two inside the quotation marks. To the human eye the two inputs can look nearly the same, especially if a display wraps text or draws the escape as a break. The parser sees a control character in the middle of a string and returns an invalid JSON syntax error. The error message is generic, so it names the syntax problem without pointing at the exact character. Comparing the failing input with the valid one, which differs only at that position, is the fastest way to find the cause in a larger document. This is the expected result for this input. The fix is to replace the physical break with the two-character escape, or to put the line break outside the string if it was only meant as layout.
Layout newlines are a separate matter
Pretty-printing inserts line breaks between properties and indents them, which the formatter does when you choose an indent such as 2. Those breaks sit between tokens and carry no meaning. They never alter what a string contains. The formatter also leaves the spelling of each string as it was written, so an escape such as backslash n stays as backslash n and does not turn into a physical break in the formatted output. Mixing up layout whitespace with string content is the source of many newline problems. A useful test is to ask whether the break is inside a pair of quotation marks. If it is, it belongs to the data and must be an escape. If it is outside, it is layout and can be added or removed freely without changing the meaning of the document.
Other escapes follow the same rule
A tab inside a string is written as backslash t, a carriage return as backslash r, and a double quote as backslash followed by a quote. A literal backslash is written as two backslashes. Characters outside the basic range can be written as backslash u followed by four hexadecimal digits, so an e with an acute accent can appear as backslash u00e9. The formatter keeps whichever spelling you used. Raw tab characters and raw carriage returns inside a string are rejected for the same reason that a raw line feed is. Windows line endings, a carriage return followed by a line feed, therefore need two escapes inside a string, written as backslash r backslash n, if the exact pair must be kept.
Where raw breaks sneak in
Raw breaks usually come from copying text through several layers. A multiline note pasted into a text field may arrive with real breaks. A programming language string literal may or may not process backslash escapes before the text reaches a JSON field, so a backslash n typed in code can become a real newline by the time the JSON is built. Editors that wrap long lines can also make one physical line look like two. When a document fails, check how the break was entered and which layer interpreted the backslash before the text reached this tool.
Fixing a failing document
Work from a minimal copy when the document is large, keeping only the property that fails. Find the position the error points to, or search the text for places where a string continues onto a new line. Replace each physical break inside a string with backslash n, keeping the surrounding quotes. If a long string is easier to maintain in several lines, the source document is probably in a different format, such as a configuration dialect or a programming language, and should be converted by a tool that knows its rules. Retest with the formatter after each repair to confirm that the document now passes. If many strings fail at once, the break was probably introduced by one step in your process, and fixing that step is better than patching each value by hand.
What this pair demonstrates
The two inputs test the strict syntax rule for control characters inside strings. They do not say how any particular application should treat a note with several lines, how long it can be, or whether the line break should be stored at all. The valid result confirms syntax only. Whether a downstream system accepts a string with a break is a separate question for its own documentation.
Newlines in text that people will read
Notes with line breaks are common in support tickets, address blocks, product descriptions and chat logs. Storing them in JSON is fine as long as the writer produces the escape. The reader then decides how to show it. A web page that inserts the string into HTML will collapse the break into a space unless it is converted or styled to preserve it, and a terminal will print a real second line. Test the display path with a short string such as one backslash n two before trusting a long field, and view the result in every place the text will appear, including exports and emails. If the same text needs to travel through CSV as well, remember that CSV handles line breaks inside quoted fields instead.
Reproduce the checks
Escaped string newline
Open the Json Validator Formatter and enter:
- JSON input
{"note":"one\ntwo"}- Indent
0
Expected output:
Valid
trueFormatted
{"note":"one\ntwo"}The JSON source contains the escape characters.
Raw newline inside the string
Open the Json Validator Formatter and enter:
- JSON input
{"note":"one two"}- Indent
0
Expected rejection:
Invalid JSON syntaxThis input contains a physical newline between quotation marks.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.
Sources
- Primary definition and convention reference ↗ · 2026-10-08