← Back to JSON Viewer

Free JSON Escape / Unescape Tool Online 2026

Escape or unescape JSON strings for APIs, databases, and code. Handle special characters properly.

JSON Escaping Guide: Handling Special Characters

What is Character Escaping and Why It's Needed

Character escaping is the process of converting special characters into a format that doesn't conflict with syntax. In JSON, certain characters have special meaning—quotes define strings, backslashes start escape sequences, newlines would break single-line strings. When you need these characters inside a JSON string value, they must be escaped.

For example, if you want a JSON string to contain a quote character: "He said \"Hello\"". The inner quotes are escaped as \" so the JSON parser knows they're part of the string content, not the end of the string. Without escaping, "He said "Hello""would cause a syntax error—the parser would think the string ends at the second quote.

Special Characters That Need Escaping

JSON requires these characters to be escaped when used inside string values:

  • Quotation mark ("): Escaped as \" because quotes delimit strings
  • Backslash (\\): Escaped as \\\\ because backslash starts escape sequences
  • Newline (line feed): Escaped as \\n because JSON strings must be single-line
  • Carriage return: Escaped as \\r for the same reason
  • Tab: Escaped as \\t for consistent whitespace representation
  • Form feed: Escaped as \\f (rarely used but part of JSON spec)
  • Backspace: Escaped as \\b (also rare)

Escape Sequences: The Complete List

Common Escape Sequences:

  • \\n - Newline (line feed)
  • \\r - Carriage return
  • \\t - Tab
  • \\" - Double quote
  • \\\\ - Backslash

Less Common:

  • \\f - Form feed
  • \\b - Backspace
  • \\uXXXX - Unicode character
  • \\/ - Forward slash (optional)

Unicode Escaping

For characters outside standard ASCII, JSON supports Unicode escape sequences: \\uXXXXwhere XXXX is the four-digit hexadecimal Unicode code point. For example:

  • \\u00A9 represents © (copyright symbol)
  • \\u2764 represents ❤ (heart emoji)
  • \\u4E2D represents 中 (Chinese character)

Modern JSON parsers handle UTF-8 directly, so Unicode escaping is optional for most characters. However, it's useful for ensuring compatibility with systems that don't support UTF-8 or for representing control characters that can't be typed directly.

Use in String Literals

The most common use case for JSON escaping is embedding JSON within string literals in programming languages. When you assign JSON to a JavaScript string variable, you need to escape it so the JavaScript parser doesn't misinterpret the quotes:

// Without escaping - SYNTAX ERROR
const jsonStr = "{"name": "John"}";  // Breaks at first inner quote

// With escaping - CORRECT
const jsonStr = "{\"name\": \"John\"}";  // Properly escaped

This is why APIs returning JSON as strings (in error messages, logs, or nested responses) need escaping. Our tool automates this process so you don't have to manually add backslashes.

Mode:

Raw JSON Input

Escaped Output

Common Use Cases for JSON Escaping

Embedding JSON in JavaScript/HTML

When you need to embed JSON data directly in HTML or JavaScript code, escaping is essential. Unescaped quotes would break the syntax. Common scenario: passing server data to client-side JavaScript.

<!-- Without escaping - BREAKS -->
<script>
  const data = "{"name": "John", "role": "Developer"}";
</script>

<!-- With escaping - WORKS -->
<script>
  const data = "{\"name\": \"John\", \"role\": \"Developer\"}";
  const obj = JSON.parse(data);
</script>

Modern frameworks (React, Vue, Angular) handle this automatically, but when writing vanilla JavaScript or server-rendered HTML, you need to escape JSON strings manually.

SQL Queries with JSON

Storing or querying JSON in SQL databases requires proper escaping to prevent syntax errors and SQL injection. When inserting JSON into a VARCHAR column or using PostgreSQL's JSONB functions, quotes must be escaped.

-- Without escaping - SQL ERROR
INSERT INTO users (data) VALUES ('{"name": "John", "bio": "Developer at "Acme Corp""}');

-- With escaping - WORKS
INSERT INTO users (data) VALUES ('{"name": "John", "bio": "Developer at \"Acme Corp\""}');

Most database libraries (like node-postgres, psycopg2, JDBC) handle escaping automatically when you use parameterized queries. But when building raw SQL strings, you must escape manually.

Command-Line Arguments

Passing JSON to command-line tools (cURL, CLIs, scripts) requires escaping because shells interpret quotes and special characters. Without escaping, the shell breaks the JSON into multiple arguments.

# Without escaping - Shell interprets quotes incorrectly
curl -X POST -d '{"name": "John", "message": "Hello "world""}' api.com/users

# With escaping - Works correctly
curl -X POST -d '{"name": "John", "message": "Hello \"world\""}' api.com/users

# Alternative: Use single quotes for outer string, double inside
curl -X POST -d '{"name": "John", "message": "Hello \"world\""}' api.com/users

API Request Bodies

When sending JSON as part of another JSON payload (nested JSON), the inner JSON must be escaped and sent as a string. Common in webhook configurations, dynamic queries, or meta-APIs.

{
  "action": "create_user",
  "payload": "{\"name\": \"John\", \"email\": \"john@example.com\"}"
}

The outer JSON is properly formatted, but the payload value is an escaped JSON string that the receiving API will parse separately.

Template Strings

Template literals in JavaScript (backticks) preserve newlines and special characters, but when you need to embed those templates as JSON strings, escaping is required. Use escaping to convert multi-line template strings into valid JSON-encoded strings.

Programming Language Examples

JavaScript/TypeScript Escaping

JavaScript provides JSON.stringify() for automatic escaping:

// Automatic escaping with JSON.stringify
const data = { name: "John", bio: 'Developer\nLoves "coding"' };
const jsonString = JSON.stringify(data);
console.log(jsonString);
// Output: {"name":"John","bio":"Developer\\nLoves \\"coding\\""}

// Embed in HTML attribute
const html = `<div data-user='${jsonString}'></div>`;

// Manual escaping function (if needed)
function escapeJson(str) {
  return str
    .replace(/\\/g, '\\\\')
    .replace(/"/g, '\\"')
    .replace(/\n/g, '\\n')
    .replace(/\r/g, '\\r')
    .replace(/\t/g, '\\t');
}

Python JSON Escaping

Python's json module handles escaping:

import json

# Automatic escaping
data = {"name": "John", "bio": "Developer\nLoves \"coding\""}
json_string = json.dumps(data)
print(json_string)
# Output: {"name": "John", "bio": "Developer\\nLoves \\"coding\\""}

# Unescape JSON string
escaped = '{\"name\": \"John\"}'
unescaped = json.loads(f'"{escaped}"')  # Wrap in quotes to parse as string
# Or use replace
unescaped = escaped.replace('\\"', '"').replace('\\\\', '\\')

Java/C# Escaping

Java and C# use similar approaches with their JSON libraries:

Java (using Jackson):

import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();
Map<String, String> data = Map.of(
    "name", "John",
    "bio", "Developer\nLoves \"coding\""
);
String jsonString = mapper.writeValueAsString(data);
System.out.println(jsonString);

C# (using System.Text.Json):

using System.Text.Json;

var data = new {
    name = "John",
    bio = "Developer\nLoves \"coding\""
};
string jsonString = JsonSerializer.Serialize(data);
Console.WriteLine(jsonString);

Shell Script Usage

In bash scripts, use tools like jq for safe JSON manipulation:

#!/bin/bash

# Using jq to properly escape
name="John"
bio="Developer\nLoves \"coding\""

# jq handles escaping automatically
json=$(jq -n \
  --arg name "$name" \
  --arg bio "$bio" \
  '{ name: $name, bio: $bio }')

echo "$json"
# Output: {"name":"John","bio":"Developer\nLoves \"coding\""}

# Send to API
curl -X POST -H "Content-Type: application/json" -d "$json" api.com/users

What is JSON Escaping?

JSON escaping converts special characters in JSON strings to their escaped equivalents, making them safe to use in APIs, databases, and code. This is essential when embedding JSON inside other formats.

Common Escape Sequences

\n = New line
\t = Tab
\" = Double quote
\\ = Backslash
\r = Carriage return

When to Use JSON Escape/Unescape

Escape JSON When:

  • Storing JSON in SQL databases
  • Sending JSON in URL parameters
  • Embedding JSON in HTML attributes
  • Using JSON in JavaScript strings
  • Logging JSON to files

Unescape JSON When:

  • Reading JSON from databases
  • Parsing API error messages
  • Debugging escaped responses
  • Converting logs to readable JSON
  • Processing escaped JSON strings

Example: Before & After

Original JSON:

{ "message": "Hello\nWorld", "path": "C:\\Users\\Documents" }

Escaped (for SQL or API):

{\"message\": \"Hello\\nWorld\", \"path\": \"C:\\\\Users\\\\Documents\"}

Common Use Cases in 2026

API Development

Escape JSON before sending in POST requests or storing in databases

Database Storage

Properly escape JSON before inserting into SQL VARCHAR or TEXT columns

JavaScript Code

Embed JSON safely in JavaScript string variables

Log Analysis

Unescape JSON from log files for easier reading and debugging

Privacy & Security

  • Client-Side Only: All escaping happens in your browser
  • No Server Upload: Your data never leaves your device
  • Safe for Sensitive Data: Process confidential JSON locally

Related Tools

📚 Learn More About JSON in Programming

Deepen your understanding of JSON string handling and programming techniques: