Skip to content
StringToJSON

Escape

Escape and unescape text for JSON.

Convert quotes, backslashes, newlines and control characters into their escape sequences — or turn a mangled escaped blob back into readable text.

Raw text

0 B

Escaped text

0 B

Output appears here.

Ready. Paste something above.

Everything runs locally in your browser — your data never leaves this page. Press⌘/Ctrl +Enter to run.

What gets escaped

JSON strings are double-quoted, so a handful of characters cannot appear literally inside them. Escaping replaces each one with a backslash sequence.

CharacterEscapeWhy
"\"Would end the string early
\\\Starts an escape sequence
newline\nControl characters are illegal raw
carriage return\rControl characters are illegal raw
tab\tControl characters are illegal raw
backspace\bControl characters are illegal raw
form feed\fControl characters are illegal raw
U+0000–U+001F\u00XXAny remaining control character

Where you need this

  • Embedding a multi-line SQL query or template as a single JSON config value.
  • Putting a Windows path such as C:\logs\app.log into a payload — every backslash has to double.
  • Building a request body by hand in a shell script or an HTTP client.
  • Reading a log line where an entire response was escaped into one field.

Escaping is not encoding

JSON escaping, URL encoding and HTML entity encoding solve different problems and are not interchangeable. A value going into a query string needs encodeURIComponent; a value rendered into markup needs HTML escaping; a value going inside a JSON string needs what this tool does. Applying the wrong one produces text that looks almost right and breaks at the worst moment.

Escaping in your own code

// JavaScript — escape, then trim the outer quotes
JSON.stringify(text).slice(1, -1)

# Python
json.dumps(text)[1:-1]

// Java
JSONObject.quote(text);        // keeps the quotes

// C#
JsonEncodedText.Encode(text);

Every one of these delegates to the language's own JSON serializer rather than doing a hand-rolled find-and-replace. That matters: a manual replace('"', '\\"') misses control characters, mishandles backslashes that were already escaped, and produces output that parses in testing and fails on the first tab character in production.

Unescaping safely

In the other direction, resist the temptation to reach for eval orast.literal_eval on untrusted text. Wrapping the body in quotes and running it through the JSON parser — what this page does — is both safer and stricter. If the input is a full JSON document rather than a bare string, the string to JSON converterhandles it and formats the result at the same time.

Escaping is not encryption, or sanitisation

A JSON-escaped string is trivially reversible and offers no protection whatsoever. More importantly, escaping for JSON does nothing to make a value safe in any other context. Text that is correctly escaped as a JSON string can still be a SQL injection when concatenated into a query, still be an XSS payload when written into a page, and still be a command injection when passed to a shell. Each destination needs its own escaping, applied at the point of use.

The one rule that prevents nearly all of these mistakes: escape at the boundary, once, using the mechanism belonging to that boundary — parameterised queries for SQL, the framework's templating for HTML, an argument array rather than a string for subprocesses. Escaping early and carrying the escaped form around is how values end up double-escaped, with \\n appearing literally in your output.

Double escaping, and how to spot it

If you see \\\\n or \\\\" in a payload, a value was escaped twice — usually because an already-serialized string was handed to a serializer again. Run it throughUnescape once and check: if the result still contains backslash sequences, peel another layer. The string to JSON converter does this automatically when the result is a full document.

Frequently asked questions

How to remove escape characters from a JSON string?
Use Unescape in the direction toggle above. Every \" becomes a quote, every \n a newline, every \t a tab. If the input is a full JSON document rather than arbitrary text, the String to JSON converter parses and pretty-prints it instead.
How to remove backslashes from a JSON string?
Those backslashes are escape characters, not extra data. Unescape the text here, or parse it as JSON — JSON.parse, json.loads, Jackson, or JsonSerializer.Deserialize — and they disappear. If the value is double-encoded, unescape or parse twice. For C# specifically see how to remove backslashes from a JSON string in C#.
What is the difference between this and the JSON to String tool?
This one escapes any text — a log line, a SQL query, an HTML fragment — without requiring it to be valid JSON. JSON to String parses and minifies a JSON document first, then escapes the result.
Does it escape forward slashes?
No. / may be escaped as \/ in JSON but never has to be. Leaving it alone keeps URLs readable. If you need the escaped form for an old parser, add the backslashes by hand.
Unescaping fails on my input. Why?
An escaped string has to be well-formed: a lone trailing backslash, or a \u without four hex digits after it, cannot be decoded. Check that you copied the entire literal, including its final characters.