← All JSON guides

JSON guide · 2 min read

JSON Escape and Unescape: Quotes, Backslashes and Newlines

Understand JSON escaping, decode nested string representations and stop confusing a JSON document with a string that contains serialized JSON.

By JSON HearthPublished 2026-08-05Updated 2026-08-22

JSON strings have their own escape syntax

Double quotes delimit JSON strings, so a literal quote inside a string must be escaped. A backslash introduces escape sequences such as newline and tab and must itself be escaped when a literal backslash is required.

{
  "quote": "She said \"hello\"",
  "message": "first line\nsecond line"
}

A JSON document is not a JSON-encoded string

An object document and a string whose value contains serialized JSON are different layers. Embedding a complete JSON document inside another string adds another level of quote and backslash escaping, which is why logs and message envelopes can look heavily escaped.

Object document:
{"name":"Ada"}

String containing JSON text:
"{\"name\":\"Ada\"}"

Recognize repeated encoding

If each component serializes text that is already serialized, every layer adds more escaping. Track which boundary owns serialization and which boundary owns parsing so data is encoded exactly once for each protocol layer.

Do not blindly remove backslashes

Global replacement can corrupt legitimate escape sequences. Decode one well-defined representation layer with a JSON parser, inspect the result and repeat only if the application contract intentionally contains nested serialized JSON.

The outer programming language can add another layer

A JSON example written inside JavaScript, Java, shell syntax or another JSON document may need escaping for both the outer language and JSON. Count representation layers before deciding how many backslashes are correct.

Use escape and unescape as a diagnostic workflow

Identify whether you have a JSON document or an encoded string, decode one layer, then validate the result. The goal is not to remove every backslash; it is to produce the representation expected by the next consumer.

  • Identify the outer representation first.
  • Decode one layer at a time.
  • Validate after decoding.
  • Avoid global search-and-replace for escape characters.
  • Keep the original while debugging logs or messages.

Common questions

Frequently asked questions

Why does my JSON contain so many backslashes?

It is probably serialized inside another string or encoded more than once. Each representation layer escapes characters required by the outer layer.

Does unescaping automatically parse the JSON object?

Not necessarily. Removing one string-encoding layer can return JSON text that still needs syntax validation and parsing.

Can I just remove every backslash?

No. Some backslashes are required JSON escapes. Decode one known representation layer with a parser or dedicated tool instead.