JSON Escape / Unescape Tool

Escape special characters in JSON strings or unescape previously escaped JSON. Handles backslashes, double quotes, newlines, tabs, Unicode sequences, and all control characters defined by the JSON specification. Free and instant.

What are JSON Escape Characters?

The JSON specification (ECMA-404 / RFC 8259) requires that string values inside a JSON document use double quotes as delimiters. This creates a conflict: if a string value itself contains a double quote, a backslash, or a control character like a newline, the JSON parser will misread it unless those characters are escaped.

Escaping means inserting a backslash (\) before the special character, transforming it into a two-character escape sequence that the parser knows to treat as a literal character rather than as JSON syntax.

For example, this is invalid JSON because the inner quote breaks the string boundary:

{ "message": "He said "hello" to me" }

This is the valid, escaped version:

{ "message": "He said \"hello\" to me" }

JSON Escape Sequences Reference Table

Every escape sequence defined by the JSON specification:

Character Escape Sequence Description
"\"Double quote
\\\Backslash (reverse solidus)
newline\nLine feed (LF, U+000A)
carriage return\rCarriage return (CR, U+000D)
tab\tHorizontal tab (U+0009)
backspace\bBackspace (U+0008)
form feed\fForm feed (U+000C)
any Unicode\uXXXXUnicode code point (4 hex digits)
/\/Forward slash (optional, but valid)

Unicode escapes use the form \uXXXX where XXXX is a four-digit hexadecimal code point. For example, \u00e9 represents the character Γ©, and \u4e2d represents the Chinese character δΈ­. Surrogate pairs (\uD800–\uDFFF) are used to encode characters outside the Basic Multilingual Plane.

When Do You Need JSON Escaping?

JSON escaping is required in several practical situations:

Common Use Cases: Embedding JSON in JSON, SQL, and HTML

Embedding JSON inside a JSON string

This pattern appears frequently in logging, message queues, and API gateways that wrap inner payloads as strings. The inner JSON object must have all its double quotes escaped:

{
  "event": "user_action",
  "payload": "{\"userId\":42,\"action\":\"login\",\"timestamp\":\"2026-03-05T10:00:00Z\"}"
}

JSON strings inside SQL

When building SQL INSERT or UPDATE statements that store JSON in a TEXT/JSONB column, you need to escape the JSON string for the SQL context as well as ensure the JSON itself is valid:

INSERT INTO logs (data) VALUES ('{"user":"alice","action":"login"}');

PostgreSQL's JSONB column accepts valid JSON directly. MySQL requires the string to be properly quoted within the SQL statement. Always use parameterized queries to avoid both SQL injection and escaping issues.

JSON data attributes in HTML

Embedding JSON in HTML data-* attributes requires HTML-encoding the double quotes as ":

<div data-config='{"theme":"dark","lang":"en"}'></div>

<!-- Or with double-quote attributes, HTML-encode: -->
<div data-config="{&quot;theme&quot;:&quot;dark&quot;}"></div>

Using single quotes for the HTML attribute (as in the first example) avoids the need to HTML-encode the inner JSON double quotes, which is the simpler approach when embedding JSON directly in HTML.

JavaScript string literals containing JSON

When writing JSON inside a JavaScript string (rather than a JS object literal), the quotes need escaping:

// Correct: use JSON.stringify instead of manual escaping
const jsonString = JSON.stringify({ user: "alice", active: true });
// Produces: '{"user":"alice","active":true}'

Frequently Asked Questions

Why do I need to escape JSON strings?

JSON strings must be enclosed in double quotes, so any double quote inside the string value must be escaped as \" to prevent the JSON parser from treating it as the end of the string. Similarly, backslashes, newlines, and control characters must be escaped so the JSON remains valid and parseable by any standards-compliant parser.

What characters need to be escaped in JSON?

The JSON specification requires escaping of: double quote (\"), backslash (\\), and control characters including newline (\n), carriage return (\r), horizontal tab (\t), form feed (\f), and backspace (\b). Any Unicode character can also be escaped as \uXXXX using its four-digit hex code point.

What is the difference between escaping and encoding in JSON?

JSON escaping refers to adding backslash sequences inside a JSON string value so special characters are treated as literal text rather than syntax. Encoding often refers to serializing an entire value or object into a JSON string representation (for example JSON.stringify in JavaScript). They solve different problems: escaping makes a string safe inside JSON, while encoding converts a data structure into a JSON-formatted string.

How do I embed JSON inside another JSON string?

To embed JSON as a string value inside another JSON document, you must escape all double quotes with \" and all backslashes with \\. For example, the JSON object {"key":"value"} becomes the string "{\"key\":\"value\"}" when embedded. Our escape tool handles this automatically β€” paste your inner JSON and it produces the correctly escaped string.

Escape or unescape JSON strings instantly

Free, browser-only tool. Your data is never sent to a server.

JSON Escape Character Reference

JSON requires certain characters within strings to be escaped with a backslash. Understanding these rules is critical when embedding JSON in HTML, URLs, or shell commands.

Required Escape Sequences

CharacterEscapedNotes
"\"Double quote β€” delimiter for JSON strings
\\\Backslash β€” escape character itself
/\/Forward slash β€” optional but recommended in HTML
Newline\nLine feed character (U+000A)
Tab\tHorizontal tab (U+0009)
Carriage return\rCarriage return (U+000D)
Backspace\bBackspace (U+0008)
Form feed\fForm feed (U+000C)
Unicode\uXXXXAny Unicode code point (4 hex digits)

Escape Example

// Raw string
He said "Hello\nWorld" and left

// JSON escaped
"He said \"Hello\\nWorld\" and left"

// JavaScript
const escaped = JSON.stringify(rawString);
const unescaped = JSON.parse(escaped);

When to Escape JSON

Also useful: JWT Decoder | JSON Validator | JSON Formatter | JSONPath Tutorial | JSON Schema Examples

JSON Escape Sequences Reference

Every escape sequence defined in the JSON specification (RFC 8259), with Unicode code points and usage notes:

Escape Sequence Character Unicode Description
\" Double quote U+0022 Required inside JSON string values
\\ Backslash U+005C Literal backslash character
\/ Forward slash U+002F Optional but valid; safe for HTML in JSON
\n Newline U+000A Line feed / newline character
\r Carriage return U+000D Used in Windows-style line endings
\t Tab U+0009 Horizontal tab
\b Backspace U+0008 Backspace character
\f Form feed U+000C Form feed / page break
\uXXXX Unicode any Any Unicode code point as 4-digit hex

Common JSON Escaping Scenarios

Real-world JSON escaping comes up in three situations that developers encounter regularly. Here are canonical examples with before and after:

1. Embedding JSON inside JSON

When an inner JSON object needs to be stored as a string value inside an outer JSON document (common in logging, message queues, and API gateways):

// Original JSON to embed
{"name": "Alice", "role": "admin"}

// Embedded as a string value in outer JSON
{"config": "{\"name\": \"Alice\", \"role\": \"admin\"}"}

2. API response with special characters

Strings containing newlines and quotes must use escape sequences to remain valid JSON:

// String with newlines and quotes
{"message": "Line 1\nLine 2\nShe said \"hello\" to him."}

3. Regex pattern in JSON

Regex patterns often contain backslashes that must be double-escaped in a JSON string:

// Backslashes in regex must be double-escaped in JSON
{"pattern": "^\\d{4}-\\d{2}-\\d{2}$"}
// This represents the regex: ^\d{4}-\d{2}-\d{2}$

JSON Escape vs URL Encode vs HTML Encode

These three encoding methods solve different problems and are often confused. Use this table to choose the right one:

Aspect JSON Escape URL Encode HTML Encode
Purpose Make string safe inside JSON Make string safe in URL query params Make string safe in HTML
Double quote \" %22 &quot;
Space Kept as-is %20 or + Kept as-is
Ampersand Kept as-is %26 &amp;
Newline \n %0A &#10;
Backslash \\ %5C Kept as-is
Use case JSON string values URL query strings HTML attribute values

How to Unescape a JSON String Online

When you receive a JSON payload that was double-encoded β€” or a JSON string stored inside another JSON string β€” it contains visible escape sequences like \n, \", \\. Unescaping converts these back to their literal characters.

  1. Paste the escaped JSON string into the input above
  2. Select Unescape mode
  3. Click Convert β€” all escape sequences are replaced with their real characters instantly

Unescape JSON String β€” Before & After

Escaped (Input) Unescaped (Output)
{\"name\":\"Alice\",\"city\":\"New York\"}{"name":"Alice","city":"New York"}
Line 1\nLine 2\nLine 3Line 1
Line 2
Line 3
path\\to\\filepath\to\file
\u0048\u0065\u006C\u006C\u006FHello

JSON String Escape & Unescape in Code

Every major language provides built-in methods for JSON string escaping and unescaping:

JavaScript

// Escape a string for embedding in JSON
const escaped = JSON.stringify(rawString);

// Unescape a JSON string back to plain text
const unescaped = JSON.parse('"' + escapedString + '"');

// Example
const raw = 'Hello "World"\nLine 2';
const esc = JSON.stringify(raw); // "Hello \"World\"\nLine 2"
const back = JSON.parse(esc);    // Hello "World"\nLine 2

Python

import json

raw = 'Hello "World"\nLine 2'
escaped = json.dumps(raw)          # '"Hello \\"World\\"\\nLine 2"'
unescaped = json.loads(escaped)    # Hello "World"\nLine 2

PHP

<?php
$raw = 'Hello "World"' . "\nLine 2";
$escaped = json_encode($raw);        // "Hello \"World\"\nLine 2"
$unescaped = json_decode($escaped);  // Hello "World"\nLine 2

Also useful: JSON Validator | JSON Stringify / Parse | Base64 JSON | JSON Repair

Related Tools

JSON Validator
Check JSON syntax and validity
JSON Pretty Print
Format and indent JSON for readability
JSON Stringify
Convert JSON objects to strings
JSON Minifier
Remove whitespace to compact JSON
JSON URL Encode
Encode JSON for use in URLs