1x and 2x Escaping
Convert quotes, backslashes, and control characters once or twice recursively for nested string placements.
json escape, double escape json, nested string escape
Escape JSON into a safe string for logs, APIs, config files, and language literals.
Escape quotes, backslashes, and control characters in your JSON strings. Supports 1x escaping, 2x double escaping, and layout-preserving formatting for nested payloads, environment variables, database strings, and source code literals.
Convert quotes, backslashes, and control characters once or twice recursively for nested string placements.
json escape, double escape json, nested string escape
Format escaped output line-by-line to preserve layout newlines, making complex payloads readable inside config files.
format escaped json, beautify escaped json
Validate JSON structure and run all escaping logic entirely in the browser for secure data handling.
local json validator, offline escape json
What is JSON Escape?
In JSON syntax, certain characters hold structural meaning. For instance, double quotes (") mark the start and end of strings, and backslashes (\) introduce escape sequences.
If you want to place raw quotes or backslashes inside a JSON string value, you must escape them. The escape process replaces structural characters with literal code sequences (e.g. \" and \\). This ensures the parser treats the text literally, avoiding parsing or syntax crashes.
Why Escaping JSON is Important
When building applications that pass JSON messages, unescaped strings can break data transfer completely. For example, database strings, API configurations, and webhooks often wrap JSON payloads within another JSON envelope. Without escaping, the child double quotes collide with the parent double quotes, throwing parser validation errors.
Double Escaping (or escaping JSON 2x) takes this one step further by escaping the backslashes of the first level of escape. It is crucial when executing shell variables in Docker runs, writing Terraform config files, or using AWS CLI scripts, where nested variables are evaluated multiple times.
Reference Table
| Character | Description | Escape Sequence |
|---|---|---|
| " | Double Quote | \" |
| \ | Backslash | \\ |
| / | Forward Slash | \/ |
| \n | Newline (Line Feed) | \n |
| \r | Carriage Return | \r |
| \t | Horizontal Tab | \t |
| \b | Backspace | \b |
| \f | Form Feed | \f |
Examples
{
"user": "developer",
"comment": "Line one\nLine two",
"quote": "JSON is \"awesome\""
}{\n \"user\": \"developer\",\n \"comment\": \"Line one\\nLine two\",\n \"quote\": \"JSON is \\\"awesome\\\"\"\n}{\\\\n \\\"user\\\": \\\"developer\\\",\\\\n \\\"comment\\\": \\\"Line one\\\\\\\\nLine two\\\",\\\\n \\\"quote\\\": \\\"JSON is \\\\\\\"awesome\\\\\\\"\\\"\\\\n}Language Code
// Escape JSON using JSON.stringify
const obj = { msg: 'Hello "World"\nNew Line' };
const escaped = JSON.stringify(JSON.stringify(obj)).slice(1, -1);
console.log(escaped); // double-escaped string# Escape JSON using json.dumps
import json
data = { "msg": 'Hello "World"\nNew Line' }
escaped_1x = json.dumps(json.dumps(data))[1:-1]
print(escaped_1x)// Escape JSON using json.Marshal
package main
import (
"encoding/json"
"fmt"
)
func main() {
m := map[string]string{"msg": "Hello \"World\""}
b, _ := json.Marshal(m)
// Marshal wraps string with quotes, strip them
escaped, _ := json.Marshal(string(b))
fmt.Println(string(escaped[1 : len(escaped)-1]))
}# Passing escaped JSON in cURL command
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-d "{\"user\":\"admin\",\"action\":\"login\"}"Comparisons
JSON Escaping targets formatting internal quotes and backslashes to render strings literal safely. JSON Encoding compiles standard compiler structures (maps, arrays) into unified serialized strings.
In JS, JSON.stringify() generates a JSON string wrapped in outer quotes ("..."). Our escape tool does not wrap the output in quotes, leaving a clean string sequence ready for direct insertion.
What are common mistakes?
Best practices
Workflow
Insert your raw structured JSON inside the input editor.
Ensure the JSON validator shows the input is syntactically correct.
Choose to escape once (1x) or twice (2x) depending on where you need it.
Format the output to review readability or minify it for compact payloads.
Get answers to common questions about JSON escaping, double escaping, formatting, and best practices.
Learn how to prettify, format, and beautify JSON in Node.js using JSON.stringify, fs module, Express middleware, third-party libraries, and online tools. Includes real code examples.
Learn how to validate JSON data using online tools, JavaScript, Python, and schema validators. Includes common JSON errors, JSON Schema basics, and best practices for developers.
Advanced JSON examples including Schema.org FAQ JSON-LD, API error responses, DynamoDB items, and MongoDB documents. Ideal for testing JSON Viewer and Formatter tools.
Explore enterprise-grade JSON examples including OpenAPI schemas, webhook payloads, Kubernetes and Docker configs, and cloud log monitoring samples for JSON Viewer testing.
Explore a big JSON sample with multiple lists, nested objects, and real-world data points. Perfect for testing JSON Viewer, JSON Formatter, and large API responses.
Explore real-world JSON samples including payment APIs, books data, database records, AWS EC2, and Azure VM examples. Perfect for testing JSON Viewer and JSON Formatter tools.
Explore more free developer tools to speed up debugging, testing, and development.