ToolDeck

JSON String Escape

Escape and unescape JSON special characters in strings

Try an example

Input

Output

Runs locally · Safe to paste secrets
Result will appear here…

What is JSON String Escaping?

JSON string escaping is the process of converting special characters inside a string into escape sequences that JSON parsers can read without breaking the structure. The JSON specification (ECMA-404 / RFC 8259) requires that certain characters inside string values be preceded by a backslash. Without proper escaping, a literal double quote or newline inside a string would terminate the string early and produce a parse error.

Every JSON string is delimited by double quotes. When the string itself contains a double quote, a backslash, or a control character (U+0000 through U+001F), the character must be replaced by its escape sequence. For example, a literal newline becomes \n, a tab becomes \t, and a double quote becomes \". Any Unicode code point can also be represented as \uXXXX, where XXXX is the four-digit hexadecimal value.

Unescaping (the reverse operation) converts those backslash sequences back into the original characters. Use it when you receive a JSON payload where string values are double-escaped, or when you need to extract raw text from a JSON log entry for display in a UI or terminal. Log aggregation pipelines hit this constantly: when a JSON-encoded message is stored as a string value inside another JSON document, every backslash in the inner string arrives doubled.

Why Use a JSON Escape Tool?

Manually adding or removing backslashes is tedious and error-prone, especially when dealing with multi-line text, file paths, or embedded code snippets. A dedicated escape tool catches edge cases that hand-editing misses.

🔒
Privacy-first processing
Your strings never leave your browser. All escaping and unescaping runs locally in JavaScript with no server calls, so sensitive data like API keys or tokens stays on your machine.
Instant conversion
Paste any text and get correctly escaped JSON output in milliseconds. No waiting for a round trip to a remote server.
🛡️
No account or install
Open the page and start escaping. No sign-up form, no browser extension, no CLI tool to install. Works on any device with a modern browser.
📋
Full character coverage
Handles all JSON-required escapes: double quotes, backslashes, control characters (U+0000 through U+001F), and Unicode sequences including emoji and CJK characters.

JSON String Escape Use Cases

Frontend development
Escape user-generated content before embedding it in a JSON payload sent via fetch or XMLHttpRequest. Prevents malformed requests when users type quotes, newlines, or emoji.
Backend API work
Build JSON response bodies in languages that do not auto-escape strings (shell scripts, SQL stored procedures, template engines). Paste the raw string, copy the escaped version.
DevOps and configuration
Embed multi-line certificate PEM blocks, SSH keys, or shell scripts into JSON config files for Terraform, CloudFormation, or Kubernetes ConfigMaps without breaking the JSON structure.
QA and testing
Create test fixtures that include edge-case characters: tabs, null bytes, Unicode surrogate pairs, and nested escaped strings. Verify that your parser handles them correctly.
Data engineering
Clean double-escaped log entries from Elasticsearch, CloudWatch, or Datadog. Unescape the string to recover the original message for analysis or re-ingestion.
Learning JSON
See exactly which characters require escaping according to the JSON spec. Useful for students working through ECMA-404 or building their own JSON parser.

JSON Escape Sequence Reference

The JSON specification defines exactly two required escapes (double quote and backslash) and six short escape sequences for common control characters. All other control characters (U+0000 through U+001F) must use the \uXXXX form. Characters above U+FFFF (like emoji) can be represented as a UTF-16 surrogate pair: \uD83D\uDE00.

CharacterDescriptionEscaped form
"Double quote\"
\Backslash\\
/Forward slash\/ (optional)
\nNewline (LF)\n
\rCarriage return\r
\tTab\t
\bBackspace\b
\fForm feed\f
U+0000–U+001FControl characters\u0000–\u001F
U+0080+Non-ASCII (e.g. emoji)\uXXXX or raw UTF-8

JSON Escaping vs. JSON Encoding

Developers sometimes confuse escaping a string with encoding a full JSON document.

String Escaping
Operates on the text inside a JSON string value. Replaces special characters with backslash sequences so the string remains valid within double quotes. Input: raw text. Output: escaped text (still needs surrounding quotes to be valid JSON).
JSON Encoding (Serialization)
Converts an entire data structure (objects, arrays, numbers, booleans, nulls) into a JSON text representation. String escaping is one step inside this larger process. Input: a data structure. Output: a complete JSON document.

Code Examples

Every major language has built-in functions for this. Examples in JavaScript, Python, Go, and jq:

JavaScript (browser / Node.js)
// JSON.stringify escapes a value and wraps it in quotes
JSON.stringify('Line 1\nLine 2')       // → '"Line 1\\nLine 2"'

// To get just the inner escaped string (no surrounding quotes):
const escaped = JSON.stringify('She said "hello"').slice(1, -1)
// → 'She said \\"hello\\"'

// Parsing reverses the escaping
JSON.parse('"tabs\\tand\\nnewlines"')  // → 'tabs\tand\nnewlines'

// Handling Unicode: emoji in JSON
JSON.stringify('Price: 5\u20ac')       // → '"Price: 5\u20ac"' (raw euro sign)
Python
import json

# json.dumps escapes and quotes a string
json.dumps('Line 1\nLine 2')           # → '"Line 1\\nLine 2"'

# Ensure ASCII: replace non-ASCII with \uXXXX sequences
json.dumps('Caf\u00e9', ensure_ascii=True)   # → '"Caf\\u00e9"'

# Keep UTF-8 characters as-is (default in Python 3)
json.dumps('Caf\u00e9', ensure_ascii=False)  # → '"Caf\u00e9"'

# Unescape by round-tripping through json.loads
json.loads('"She said \\"hello\\""')      # → 'She said "hello"'
Go
package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	// json.Marshal escapes a Go string for JSON
	raw := "Line 1\nLine 2\tindented"
	b, _ := json.Marshal(raw)
	fmt.Println(string(b))
	// → "Line 1\nLine 2\tindented"

	// Unescape with json.Unmarshal
	var out string
	json.Unmarshal([]byte(`"She said \"hello\""`), &out)
	fmt.Println(out)
	// → She said "hello"
}
CLI (jq)
# Escape a raw string into a JSON-safe value
echo 'Line 1
Line 2	with tab' | jq -Rs '.'
# → "Line 1\nLine 2\twith tab\n"

# Unescape a JSON string back to raw text
echo '"She said \"hello\""' | jq -r '.'
# → She said "hello"

Frequently Asked Questions

What characters must be escaped in a JSON string?
The JSON specification (RFC 8259) requires escaping the double quote (\"), the backslash (\\), and all control characters from U+0000 to U+001F. The forward slash (/) may be escaped as \/ but this is optional. All other Unicode characters, including non-ASCII text and emoji, can appear unescaped as long as the document uses UTF-8 encoding.
What is the difference between JSON escape and JSON stringify?
JSON.stringify() in JavaScript serializes an entire JavaScript value into a JSON string, adding surrounding double quotes and escaping special characters inside. JSON escaping refers specifically to the character-level replacement of special characters with backslash sequences. Calling JSON.stringify() on a string performs escaping as part of the serialization process.
How do I escape a newline in JSON?
Replace the literal newline character (U+000A) with the two-character sequence \n. Similarly, carriage return (U+000D) becomes \r. If you use JSON.stringify() or json.dumps() in Python, these replacements happen automatically.
Why is my JSON string double-escaped?
Double escaping happens when a string is serialized twice. For example, calling JSON.stringify() on a string that already contains escape sequences will escape the backslashes again: \n becomes \\n. To fix this, parse the string once with JSON.parse() before re-serializing, or check your pipeline for redundant encoding steps.
Can I use single quotes in JSON strings?
No. The JSON specification requires double quotes for all string values and property names. Single quotes are not valid JSON and will cause a parse error. If your source data contains single quotes, they do not need escaping inside a JSON double-quoted string because single quotes are ordinary characters in that context.
Is it safe to put unescaped UTF-8 characters in JSON?
Yes, as long as the JSON document is encoded in UTF-8, which RFC 8259 specifies as the default and recommended encoding. Characters like accented letters, CJK ideographs, and emoji can appear directly in the string without \uXXXX escaping. Some older systems expect ASCII-only JSON; in that case, use the ensure_ascii option in Python or a similar flag in your language.
How do I escape emoji in JSON?
Emoji above U+FFFF are represented in JSON using a UTF-16 surrogate pair. For example, the grinning face (U+1F600) becomes \uD83D\uDE00. Most serializers handle this automatically. If you are writing JSON by hand or your tooling only supports the Basic Multilingual Plane, use the surrogate pair notation. Otherwise, include the emoji directly as a UTF-8 character.