JSON String Escaper/Unescaper

Escape control chars and backslashes for valid JSON strings

Escape any text into a valid JSON string body, or unescape a JSON string back to raw text. Handles quotes, backslashes, and control characters as \uXXXX per the JSON spec. Runs entirely in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

Which characters must be escaped in a JSON string?

The JSON spec requires escaping the double quote and the backslash, plus all control characters below U+0020. Common controls have short escapes such as backslash-n for newline and backslash-t for tab; the rest become a six-character \uXXXX sequence.

When you embed arbitrary text inside a JSON string, certain characters must be escaped or the JSON becomes invalid. This tool escapes text into a spec-correct JSON string body and unescapes it back, so you can move data in and out cleanly.

How it works

Escaping applies the JSON string rules in order. The backslash and double quote get short escapes, the common control characters get their named escapes, and any other control character becomes a \uXXXX sequence:

\  -> \\        "  -> \"
newline -> \n   tab -> \t   carriage return -> \r
backspace -> \b  form feed -> \f
other controls (< U+0020) -> \u00XX

Unescaping reverses this: it reads each backslash sequence, expands the known escapes and \uXXXX references, and rejects malformed escapes with an error.

Complete escape table

CharacterEscaped formNotes
Double quote "\"Required — terminates the string otherwise
Backslash \\\Required — starts an escape sequence otherwise
Newline (U+000A)\nVery common in multi-line text
Tab (U+0009)\t
Carriage return (U+000D)\r
Backspace (U+0008)\bRare
Form feed (U+000C)\fRare
Other U+0000–U+001F\u00XXRequired for all control chars below space
Forward slash /\/Optional — JSON allows either form
Non-ASCII (optional)\uXXXXOnly needed for pure-ASCII output

When do you actually need this?

A few situations where this tool is particularly useful:

Building JSON by string concatenation. Generating JSON by concatenating strings is generally discouraged in favour of JSON.stringify, but when you must — for example in a shell script or a template — every user-supplied value needs its quotes and backslashes escaped before inclusion.

Embedding JSON inside an HTML script tag. The closing sequence </script> inside a JavaScript string terminates the tag early in HTML. Escaping / as \/ prevents this. This tool’s slash-escape option handles it.

Storing JSON strings in JSON. When a JSON field’s value is itself a JSON string (a common pattern in logging or event systems), every " and \ in the inner JSON must be escaped for the outer JSON parser. For example, the value {"a":1} must be stored as "{\"a\":1}".

Debugging escaped data. When you receive escaped JSON from an API and need to read the actual content, unescape it here instead of parsing it in code.

Tips and notes

The backslash must be escaped before the quote, or you would double-escape an already-escaped quote. The forward slash never needs escaping in JSON, so it is left alone by default; turn on slash-escaping only when embedding JSON inside an HTML script tag, where </ could otherwise close the tag early. For pure-ASCII output across systems with shaky encoding, enable the ASCII-safe option to emit every non-ASCII character as \uXXXX.