Unescaped Newline in JSON – Escape Line Breaks Correctly

Local only

A JSON string cannot contain a literal line break. Newlines inside a quoted value must be represented with an escape such as \n in the raw JSON text.

Indent

41 chars · 4 lines

Two-way sync · Edit either side and the other updates automatically.

Invalid JSONUnescaped control character in string
Line 2Column 25Character 27Found line break
"message": "first line

JSON string control-character error

Why literal newlines break JSON strings

Pretty-printed JSON may place line breaks between properties, but an actual line break inside a quoted string is different. Control characters in string content must use their escaped representation. This tool is free to use. No account or payment is required.

Last updated

What you can do here

  • Browser-local JSON processing
  • Route-specific malformed JSON example
  • Focused parser or repair guidance
  • Broken and corrected JSON example
  • Related JSON problem navigation

Steps

  1. Locate the literal line break between the opening and closing quotes.
  2. Replace the raw newline character with \n in the JSON text when a newline belongs in the value.
  3. Validate the raw JSON before embedding it in another programming-language string.

Formatting newlines and string newlines are different

Whitespace between JSON tokens can include newlines. The restriction applies only after the parser has entered a quoted string value or property name.

  • Multiline user input inserted into JSON without serialization.
  • Template output that writes raw line breaks into a quoted value.
  • Copied text pasted directly between JSON quotation marks.

Let a serializer escape text when possible

When generating JSON in code, prefer JSON.stringify or an equivalent serializer instead of manually concatenating strings. The serializer will escape embedded newline characters correctly.

Unescaped newline example

The invalid value contains a literal line break; the valid version stores the same break as \n.

Invalid multiline string

{
  "message": "first line
second line"
}

Valid escaped newline

{
  "message": "first line\nsecond line"
}

Common questions

Frequently asked questions

Can formatted JSON contain newlines?

Yes, between tokens. A literal newline is invalid only when it appears inside a quoted JSON string.

How do I store multiline text in JSON?

Represent each line break with the escaped newline sequence \n in raw JSON text, or let a JSON serializer escape the string for you.

Why does the number of backslashes change in source code?

A programming-language string can add another escaping layer. Inspect the runtime JSON text to determine what JSON.parse actually receives.

Debugging a specific JSON issue? Browse JSON troubleshooting.