How to Escape and Unescape JSON Strings (With Examples)
Learn how to properly escape and unescape JSON strings in JavaScript, PHP, Python, and other languages with real-world examples.
Jun 16, 2026
Escape or unescape JSON string content. Convert special characters to JSON-safe escape sequences and back.
JSON escaping is the process of converting special characters in a string to their JSON-safe escape sequences. According to the JSON specification (RFC 7159), certain characters must be escaped when they appear inside a JSON string: the double quote ("), backslash (\\), and control characters (U+0000 through U+001F).
For example, the string He said "Hello" becomes He said \"Hello\" when escaped for JSON. The tab character becomes \t, newline becomes \n, and carriage return becomes \r.
| Character | Escape Sequence | Description |
|---|---|---|
| " | \" | Double quote |
| \\ | \\\\ | Backslash |
| / | \\/ | Forward slash (optional) |
| \b | \\b | Backspace |
| \f | \\f | Form feed |
| \n | \\n | Newline |
| \r | \\r | Carriage return |
| \t | \\t | Tab |
| U+0000-U+001F | \\uXXXX | Control characters |
This tool produces similar output to JavaScript's JSON.stringify() but gives you more control. You can choose whether to escape forward slashes, escape non-ASCII characters as \\uXXXX, and wrap the output in double quotes.
Escaping forward slashes (\\/) is optional in JSON but was required in older JSON specifications. Some applications (especially those embedding JSON in HTML <script> tags) still escape slashes to prevent </script> injection.
The JSON specification requires that the double quote (") and backslash (\\) be escaped, and all control characters (U+0000 through U+001F) be escaped as well. Control characters are escaped as \\n, \\t, etc., or as \\uXXXX for others.
Yes. The Escape forward slashes option is especially useful for JSONP or JSON embedded in HTML <script> tags, as it prevents injection attacks via closing tags like </script>.
JSON escape converts characters to backslash sequences (\\n, \\") for use inside JSON strings. URL escape (percent encoding) converts characters to %XX sequences for use in URLs. They serve different purposes and use different encoding schemes.
Encode and decode Base64 strings
Encode and decode URL strings
Encode/decode HTML entities
Convert domain names to/from Punycode
Encode/decode text in various formats
Blog
Learn how to properly escape and unescape JSON strings in JavaScript, PHP, Python, and other languages with real-world examples.
Jun 16, 2026
Learn how to prevent SQL injection by properly escaping SQL strings and using prepared statements across PHP, Python, JavaScript, and Java.
Jun 16, 2026
A complete reference for XML escape characters including the five predefined entities, CDATA sections, and how to escape special characters in XML.
Jun 16, 2026