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
| Character | Escaped 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.txtAll 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.