Skip to tool
ecech.
💻 Developer & Code

Every Level of JSON Nesting Doubles the Backslashes

Seven characters become eleven, then nineteen. By the third level nobody can edit it by hand — which is the signal to stop nesting.

Escaped



Unescaped



Length at each level

What has to be escaped

Advertisement

How the calculation works

The same seven characters, nested original 7 chars one level 11 chars two levels 19 chars By the third level nobody can edit it by hand. That is the signal to stop nesting.

How to Use This Tool

Paste text to escape it for a JSON string, or paste an escaped string to get it back. The growth table shows what each further level of nesting costs.

What actually needs escaping

The list is shorter than most people assume. Inside a JSON string, exactly three things must be escaped:

  • Double quote\", because it would end the string.
  • Backslash\\, because it starts an escape.
  • Control characters below U+0020 — newline, tab, carriage return and the rest, either as their shorthand or as \u00XX.

That is all. Single quotes need nothing. Non-ASCII characters need nothing — JSON is Unicode and é, 中 and emoji are all legal literally. Escaping them as \u sequences is valid and makes the document larger and less readable for no benefit.

The forward slash question

A forward slash does not need escaping, and a lot of tools escape it anyway. The convention comes from embedding JSON inside an HTML <script> tag, where the sequence </ would terminate the tag early — escaping the slash prevents that.

If your JSON is going into a script tag, keep it. Everywhere else it is noise, and both forms parse identically. The option is there because you will meet both.

Required, and not required must escape " \ and control characters need not escape ' / é 中 emoji Escaping the second group is legal, larger and harder to read. The one exception is / inside an HTML script tag, where </ would end the tag.
Over-escaping is valid JSON and it makes documents bigger and diffs worse.

When the nesting is the problem

Each level roughly doubles the escaped length: 7 characters become 11, then 19, then 35. That is already unreadable at level two and unmaintainable at level three — you cannot edit it by hand without miscounting a backslash, and you cannot review a diff of it at all.

If you find yourself at two levels, the structure is usually the thing to fix rather than the escaping:

  • Store the inner document as an object, not as a string containing a document. Most databases and APIs accept nested objects directly.
  • Base64 the payload if it genuinely must be opaque text. It is larger but has no escaping at all, so it survives any number of layers unchanged.
  • Keep it in a separate field or file and reference it.

Over-escaping is the usual bug

A string that arrives as \\"hello\\" when you expected "hello" was escaped once too often — typically because a value was already a JSON string and something serialised it again. The fix is upstream: find the layer applying the second escape, rather than unescaping twice to compensate, which breaks the moment the input arrives correctly escaped.

Advertisement

Frequently Asked Questions

Which characters need escaping in JSON?
Only three kinds: the double quote, the backslash, and control characters below U+0020 such as newline and tab. Single quotes, forward slashes and non-ASCII characters like é or 中 need nothing — JSON is Unicode and they are legal literally.
Do I need to escape forward slashes in JSON?
No. The convention comes from embedding JSON inside an HTML script tag, where the sequence
Why does my JSON string have so many backslashes?
Because it has been escaped more than once. Each level of nesting escapes the previous level's escapes, so the backslash count roughly doubles each time — seven characters become eleven, then nineteen, then thirty-five.
How do I fix over-escaped JSON?
Find the layer applying the extra escape rather than unescaping twice to compensate. A double-unescape works until the input arrives correctly escaped, at which point it corrupts the data. The bug is almost always something serialising a value that was already a JSON string.
Should I escape non-ASCII characters as \u sequences?
You can, and there is rarely a reason to. JSON is Unicode, so accented letters, CJK characters and emoji are all legal literally. Escaping them produces a valid document that is larger and much harder to read, and it does not improve compatibility with anything current.
What should I do instead of nesting JSON in a string?
Store the inner document as an actual nested object, which most databases and APIs accept directly. If it genuinely must be opaque text, base64 has no escaping at all and survives any number of layers unchanged — larger, but readable at every level.

Related tools in Developer & Code

Browse all Developer & Code tools
The desk where ecech. tools get written: a laptop, a notebook of to-dos and a whiteboard listing the tools on the site.

Made by one person

ecech. is not a content farm. Every tool here is written and checked by hand, one at a time, by someone who wanted the tool to exist and could not find a version that showed its working.

No accounts and no sign-in, and nothing you type reaches a server — every calculation on this page runs inside your browser. The ads are served by Google and do set their own cookies, which is set out in full on the privacy page. More about the site.