↩️

JSON String Escaper

Escape or unescape JSON strings for use in source code. Escape mode converts JSON into a single-line string literal with proper backslash encoding. Unescape mode reverses the process back to readable JSON.

JSON String Escaper / Unescaper

Escape JSON into source-code string literals, or unescape escaped strings back to readable JSON.

How to Use the JSON String Escaper

You have a JSON document and need to embed it inside a string literal in source code. Paste the JSON into the left panel and click Escape. The tool produces a single-line string with every special character properly escaped, ready to paste into a Java, C#, Python, or any language that uses backslash-escape sequences. Switch to Unescape mode to reverse the process: paste an escaped string and get back readable, formatted JSON.

Embedding JSON in unit test fixtures

A developer writing unit tests in Java or C# escapes a JSON fixture object and pastes the result directly into a string literal within the test method. This avoids manually escaping each double quote and backslash, eliminating syntax errors from incomplete escaping.

Recovering JSON from log files

A DevOps engineer investigating a production incident finds an escaped JSON string in a log file. Using Unescape mode, they convert the log entry back to formatted JSON in one click, making it possible to inspect the original data structure and identify the root cause.

Preparing JSON for shell script variables

A developer writing a Bash or PowerShell script that passes JSON as an environment variable uses Escape mode to produce a safe, single-line string. The escaped output is guaranteed not to break the quoting of the enclosing shell script.

Converting between escaped and readable JSON

A frontend developer receives an escaped JSON string from a backend API error message and needs to inspect its structure. Pasting it into Unescape mode produces readable, indented JSON that reveals the error details without manual reformatting.

How the JSON String Escaper Works

The escape process uses double-serialisation with JSON.stringify(). The input JSON is first parsed into an object via JSON.parse(), which validates it. Calling JSON.stringify() on that object produces a JavaScript string. Calling JSON.stringify() again on that string produces the final output: a JSON string literal whose value is the original JSON document, with every special character escaped according to the ECMAScript specification.

Unescape Process

The unescape mode reverses each layer of escaping. It first strips the outer JSON string encoding via JSON.parse(), which interprets all backslash-escape sequences and returns the raw string value. If that raw string is itself valid JSON, it is parsed and re-formatted with indentation. If the unescaped result is not valid JSON, it is returned as-is. This layered approach handles both fully escaped JSON documents and partially escaped strings.

Character Escape Reference

The JSON escape specification (RFC 8259) requires escaping of double quotes, backslashes, forward slashes, backspace, form feed, newline, carriage return, tab, and all non-ASCII or non-printable Unicode characters using hexadecimal escape sequences. The tool handles all of these automatically through the native JavaScript serialiser.

Frequently Asked Questions

When would I use escape mode vs unescape mode?

Escape mode is for embedding JSON inside a string literal in source code, such as test fixtures in Java or C#, environment variables in shell scripts, or JSON payloads in Python string templates. Unescape mode is for recovering readable JSON from escaped strings found in log files, error messages, or API responses where the JSON was already escaped for transport.

What characters does the tool escape?

All characters required by the JSON specification (RFC 8259) are escaped: double quotes, backslashes, and control characters including backspace, form feed, newline, carriage return, and tab. Non-ASCII and non-printable Unicode characters are represented using hexadecimal escape sequences. The tool handles this automatically through the native JavaScript serialiser.

Can I escape invalid JSON?

No. The tool first parses the input with JSON.parse to validate it. If the input is not valid JSON, an error message is displayed showing the parser position. You must fix the JSON syntax first, then run the escape operation. Invalid input cannot be meaningfully escaped because the parser cannot determine the intended structure.

What programming languages work with the escaped output?

The escaped output is valid in any language that uses backslash-escape sequences for string literals: Java, C#, C++, JavaScript, TypeScript, Python, Go, Rust, Ruby, PHP, Kotlin, Swift, and many others. The escaping follows the universal JSON string encoding, which is compatible with the common subset of escape sequences across these languages.

Does the tool handle Unicode characters correctly?

Yes. JSON.stringify handles UTF-16 surrogate pairs natively. Emoji and other characters outside the Basic Multilingual Plane are either passed through as-is or represented as surrogate escapes. The round-trip through parse and stringify preserves all Unicode characters correctly, including multi-byte characters and emoji.

jsonescapeunescapestringencodeliteral

More Tools You Might Like

πŸ”“JSON String Un-escaper
πŸ”·JSON to TypeScript Generator
πŸ“‹CSV to JSON & XML Data Converter
πŸ“±QR Code Generator & Scanner
βœ…JSON Payload Schema Validator
✨JSON Prettifier