FormatterHQ

JSON Escape

Escape any text so it can be placed inside a JSON string value: quotes, backslashes, line breaks and control characters are converted exactly as JSON requires.

Loading tool…
Ready.

How to use the JSON Escape

  1. Type or paste into the Raw text box, or click Sample. You can also drop a text file on either box, or load one into the Escaped JSON box with its Open file button.
  2. The Escaped JSON box updates live as you type. It works both ways — edit the Escaped JSON side to convert back, or click ⇄ Swap to exchange the sides.
  3. Use Copy or Download on either box to take the result, or Clear to start again.

JSON escape for string values

JSON strings are delimited by double quotes and cannot contain raw line breaks or control characters. The JSON escape tool converts text into a valid string body following RFC 8259 — the same result as JSON.stringify(text) without the outer quotes — ready to paste between " characters.

What JSON requires

CharacterEscaped as
Double quote\"
Backslash\\
Line feed, carriage return, tab\n, \r, \t
Backspace, form feed\b, \f
Other control characters U+0000–U+001F\u00XX, e.g. \u0001

Everything else is kept as is. The single quote is not escaped — \' is actually invalid in JSON — and non-ASCII text such as é stays readable, because JSON is UTF-8 by definition.

Example

Input:

He said "hi"
	Path: C:\temp
</script> é

Output:

He said \"hi\"\n\tPath: C:\\temp\n</script> é

Common uses

  • JSON inside JSON. APIs such as AWS Step Functions, webhooks and message queues often expect a field containing a JSON document as a string. Escape the inner document here and paste it as the value.
  • Multi-line text in config files. Put a certificate, SQL query or email template into a JSON config value without breaking the file.
  • Hand-written test fixtures where a value contains quotes or Windows paths.

How other encoders differ

JSON allows some freedom, so libraries produce slightly different — but equally valid — output. Python’s json.dumps escapes every non-ASCII character as \u00e9 unless you pass ensure_ascii=False. PHP’s json_encode escapes forward slashes and non-ASCII characters by default; the JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE flags turn that off. This tool matches JavaScript’s JSON.stringify, the most compact form.

Escaping a string in code

Every mainstream language can produce the same escaped body; just strip the surrounding quotes that the encoder adds:

// JavaScript
JSON.stringify(text).slice(1, -1)

# Python: keep non-ASCII characters readable
json.dumps(text, ensure_ascii=False)[1:-1]

// PHP: without these flags, / becomes \/ and é becomes \u00e9
substr(json_encode($text, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1)

# Shell with jq: read a whole file as one JSON string
jq -Rs . message.txt

All of these produce valid JSON. Differences in slashes or \u sequences are cosmetic, since any JSON parser reads them back to the same text.

Watch out for

The forward slash is not escaped, so </script> passes through unchanged. If you embed JSON in an HTML <script> tag, escape < as \u003c or use a type="application/json" block with care. For turning a whole JSON document into a one-line string literal, the JSON Stringify tool adds the quotes for you, and JSON Unescape reverses this tool. To check the final document, run it through the JSON Validator.

The escaping runs in your browser; nothing is sent to a server.

Frequently asked questions

Do single quotes need escaping in JSON?
No. JSON strings always use double quotes, so a single quote is an ordinary character. Writing \' is invalid JSON and strict parsers reject it.
Should I escape forward slashes as \/?
It is optional. JSON allows \/ and some encoders (like PHP’s json_encode) use it by default, but it is not required. This tool leaves / unchanged.
Why must newlines be escaped in JSON?
The JSON grammar does not allow raw control characters, including line breaks, inside strings. A parser will throw an error such as “Bad control character in string literal”.
How do I escape a whole JSON object to put it in another JSON field?
Paste the object into the Raw text box. The escaped result can go between double quotes as a string value. The receiving side then parses that string a second time.

Last updated October 3, 2026

Guides

Related tools