Last updated
JSON Escape / Unescape Examples
The JSON Escape/Unescape tool converts special characters to their JSON escape sequences and back. Below are examples of common escaping scenarios.
JSON Escape Sequences Reference
Character Escaped Description
" \" Double quote
\ \\ Backslash
/ \/ Forward slash (optional)
newline \n Line feed
tab \t Horizontal tab
carriage \r Carriage return
form feed \f Form feed
backspace \b Backspace
U+0000–001F \uXXXX Control characters
Example: Escaping a String for JSON
// Input string (raw text):
He said "Hello, World!"
Path: C:\Users\Alice\Documents
Line 1
Line 2
Indented with tab
// Escaped for JSON:
"He said \"Hello, World!\"\nPath: C:\\Users\\Alice\\Documents\nLine 1\nLine 2\n\tIndented with tab"
Example: Embedding JSON Inside JSON
// Inner JSON object:
{
"name": "Alice",
"age": 30
}
// Escaped to embed as a string value in outer JSON:
{
"event": "user_created",
"payload": "{\"name\":\"Alice\",\"age\":30}"
}
// The payload field contains the inner JSON as an escaped string.
// To read it: JSON.parse(response.payload)
Example: Unescaping Double-Encoded JSON
// What you receive from an API (double-encoded):
"{\"status\":\"ok\",\"data\":{\"id\":42,\"name\":\"Alice\"}}"
// After first unescape:
{"status":"ok","data":{"id":42,"name":"Alice"}}
// Now valid JSON — parse normally:
{
"status": "ok",
"data": {
"id": 42,
"name": "Alice"
}
}
Example: Environment Variable with JSON
// Formatted JSON config:
{
"host": "localhost",
"port": 5432,
"credentials": {
"user": "admin",
"password": "s3cr3t"
}
}
// Escaped for .env file (single line):
DB_CONFIG="{\"host\":\"localhost\",\"port\":5432,\"credentials\":{\"user\":\"admin\",\"password\":\"s3cr3t\"}}"
// In application code:
const config = JSON.parse(process.env.DB_CONFIG);
Example: SQL String with JSON
// JSON to store in a database TEXT column:
{"event": "login", "ip": "192.168.1.1", "timestamp": "2024-01-15T14:30:00Z"}
// Escaped for SQL INSERT:
INSERT INTO audit_log (data) VALUES
("{\"event\": \"login\", \"ip\": \"192.168.1.1\", \"timestamp\": \"2024-01-15T14:30:00Z\"}");
// Better approach: use parameterized queries
// db.query('INSERT INTO audit_log (data) VALUES (?)', [jsonString]);
Example: Unicode Escape Sequences
// Input with special characters:
Café au lait — €5.00
日本語テスト
Emoji: 😀
// With Unicode escaping:
"Caf\u00e9 au lait \u2014 \u20ac5.00"
"\u65e5\u672c\u8a9e\u30c6\u30b9\u30c8"
"Emoji: \ud83d\ude00"
// Unescaping restores original:
Café au lait — €5.00
日本語テスト
Emoji: 😀
Example: Webhook Payload Escaping
// Webhook payload to send as a JSON string field:
{
"type": "payment",
"amount": 99.99,
"description": "Order #1234 - \"Premium\" plan"
}
// Escaped as a string value:
{
"event": "webhook_received",
"raw_payload": "{\"type\":\"payment\",\"amount\":99.99,\"description\":\"Order #1234 - \\\"Premium\\\" plan\"}"
}
// Note: the inner quotes are escaped once (\"),
// and the backslashes before them are escaped again (\\")
Example: Log File JSON Strings
// Log entry (raw):
2024-01-15 14:30:00 INFO Request: {"method":"POST","path":"/api/users","body":{"name":"Alice","email":"alice@example.com"}}
// The JSON in the log is already escaped — to read it:
// Extract the JSON portion and unescape it
// Unescaped and formatted:
{
"method": "POST",
"path": "/api/users",
"body": {
"name": "Alice",
"email": "alice@example.com"
}
}
Common Escaping Mistakes
// ✗ Wrong — unescaped quotes break JSON:
{ "message": "He said "hello"" }
// ✓ Correct — quotes escaped:
{ "message": "He said \"hello\"" }
// ✗ Wrong — unescaped backslash:
{ "path": "C:\Users\Alice" }
// ✓ Correct — backslash escaped:
{ "path": "C:\\Users\\Alice" }
// ✗ Wrong — literal newline in string:
{ "text": "line 1
line 2" }
// ✓ Correct — newline escaped:
{ "text": "line 1\nline 2" }
Paste your text into the escape tool to convert it to a valid JSON string, or paste an escaped JSON string into the unescape tool to restore the original readable text.