Encode / Decode
String Escape / Unescape
Escape or unescape a string for JSON, JavaScript, or SQL. Quotes, newlines, and backslashes follow that syntax. A bad JSON unescape shows the parser error.
Ready to run.
Escaped output
—
Output actions
Ready to copy output once your result appears.
JSON, JavaScript, and SQL literals
String Escape / Unescape rewrites one string for JSON, JavaScript, or SQL, or reverses that rewrite. JSON uses the JSON string rules and then drops the surrounding quotes. JavaScript escapes quotes, backslashes, and a few controls with backslashes. SQL doubles single quotes and backslashes. The page does not wrap the result in quotes for you.
How to escape or unescape
- 1Choose Escape or Unescape. Escape is selected at open. The field label is Raw string in Escape and Escaped string in Unescape.
- 2Set Syntax to JSON, JavaScript, or SQL. JSON is the initial choice. Changing either control does not run the conversion.
- 3Press Run. Escaped output or Unescaped output shows the text. Copy output copies it. Success says Escaped successfully. or Unescaped successfully. A JSON unescape that the parser rejects clears the result and starts the status with Error:.
What it is good for
- • Turning He said "hello" into He said \"hello\" for a JSON or JavaScript string body.
- • Doubling the quote in O'Brien so SQL sees O''Brien.
- • Putting a real newline back when the field contains the two characters backslash and n in JSON.
Example
Input
He said "hello"
Output
He said \"hello\"
Escape with JSON selected. JavaScript prints the same characters for this input, because both escape the double quote. SQL leaves the double quote alone and returns He said "hello".
What each syntax changes
JSON escape runs JSON.stringify and removes the first and last character, which are the quotes the serializer added. A double quote becomes a backslash and a quote. A backslash is doubled. A newline, carriage return, or tab becomes a backslash escape. Other control characters become \u plus four hex digits. Ordinary text stays, including an apostrophe and the letters in café. The result is the inside of a JSON string. If you need a complete JSON value, add the quotes yourself. JSON unescape wraps the field in quotes and parses it, so \u0041 becomes A and a backslash-n pair becomes a real newline.
JavaScript escape doubles backslashes first, then prefixes a backslash to ' and to ", then replaces a real newline, carriage return, and tab with \n, \r, and \t. it\'s is the JavaScript spelling of it's. A backtick and a ${ sequence are not escaped. SQL escape replaces each ' with '' and then doubles backslashes. O'Brien becomes O''Brien. A double quote is unchanged. A newline stays a newline, so a SQL string that contains a line break is not folded into \n. SQL unescape collapses '' to ' first, then collapses paired backslashes.
Escape, Unescape, and the syntax menu
The mode buttons are Escape and Unescape, in a group labeled Escape mode. Syntax is a menu with JSON, JavaScript, and SQL. The placeholder in Escape is a sample with a quote and a newline hint. The placeholder in Unescape shows a backslash form. Placeholders are not loaded into the field. The field starts empty, and Run on an empty field succeeds with an empty result in every syntax and both directions.
The result label follows the mode: Escaped output or Unescaped output. Copy output copies that box. There is no download. The status line is the only place a failure is described. JSON is the syntax that can fail on unescape. JavaScript and SQL unescape are sequences of replacements, so they do not throw when the escapes are unbalanced. They still report Unescaped successfully.
Jobs for a quoted literal
Use JSON when the next paste is the inside of a JSON string in a fixture or a log. A full document, with indent, belongs on the JSON formatter after you have a complete value. Nested JSON that is still wrapped as a string belongs on JSON unescape, which decodes a JSON string value rather than only the escape rules in this field. This page will not pretty-print braces.
HTML entities such as " belong on HTML encode / decode. A \u escape of every character, including letters that JSON would leave alone, belongs on Unicode escape. JSON escape of café stays café. The Unicode tool would spell each character. A SQL statement that needs indenting, rather than a quoted literal, belongs on the SQL formatter. This page does not see SELECT or table names.
JSON errors and the JavaScript backslash order
JSON unescape of a raw quote fails because the added quotes no longer wrap one string. Unescape of He said "hello" in JSON clears the output and sets the status to Error: Unexpected non-whitespace character after JSON at position 10 (line 1 column 11). That sentence is the parser's message with Error: in front. A truncated \u or a stray backslash at the end fails the same way, with whatever message the parser returns. The previous success text is not kept.
JavaScript unescape applies \n, \r, \t, \", and \' before it turns \\ into a single backslash. A doubled backslash followed by the letter n is therefore read as a backslash plus a newline, not as a backslash plus n. Escaping a backslash and the letter n, then unescaping that result, does not return the original two characters. JSON does not have that ordering bug: its parser treats \\n as a backslash and the letter n. If the round trip must preserve a Windows path or a regex that contains \n as two source characters, use the JSON syntax, or check the JavaScript result before you paste it into code.
Rules and limitations
JSON escape is a JSON string body, without wrapping quotes. JSON unescape parses that body and accepts \u escapes. JavaScript escapes backslash, quotes, newline, carriage return, and tab. SQL doubles single quotes and backslashes only.
A failed JSON unescape clears the result and prefixes Error: to the parser message. JavaScript unescape can misread a doubled backslash before n. Empty input succeeds. Copy output copies the result. There is no download and no HTML mode.
Common problems
If SQL left a double quote untouched, that is the SQL rule. Only single quotes and backslashes change. Use JavaScript or JSON when the double quote must gain a backslash.
If a JavaScript round trip turned \n into a backslash and a real newline, the unescape order did that. Switch Syntax to JSON for a round trip that keeps the letter n.
Frequently asked questions
Does JSON mode include the surrounding quotes?
No. Escape removes the quotes that JSON.stringify adds. You get the inside of the string. Add quotes yourself if the destination wants a full JSON literal.
How does SQL treat an apostrophe?
Each single quote becomes two single quotes. O'Brien escapes to O''Brien. A double quote is left as a double quote. A newline stays a newline.
Why did JavaScript unescape change a backslash and n?
The \n replacement runs before the \\ replacement. Two backslashes and the letter n become one backslash and a newline. JSON unescape does not do that.
What is the status when JSON unescape fails?
The result is cleared. For He said "hello" the status is Error: Unexpected non-whitespace character after JSON at position 10 (line 1 column 11).
Is my input uploaded or saved?
This tool processes input in your browser and does not save its input or output to local storage. Copy and download are user actions. The site uses analytics for page and action metadata and may use ads and production error reporting; see the Privacy Policy for those services.
About this tool
Maintained by Toolbench. For large or deeply nested files, split the input into smaller batches and verify representative results before using them in an import or application. Report reproducible problems through Contact.
Next steps
Related tools
Continue the workflow with nearby tools for formatting, publishing, debugging, or follow-up checks.
JSON Formatter
Pretty-print JSON with 2 or 4 spaces, or minify it.
Open JSON FormatterJSON Unescaper
Unwrap quoted JSON strings, including nested layers, into text or formatted JSON.
Open JSON UnescaperHTML Encode / Decode
Encode brackets, ampersands, and quotes as HTML entities, or decode them.
Open HTML Encode / DecodeUnicode Escape / Unescape
Turn text into Unicode escapes, or turn those escapes back into text.
Open Unicode Escape / UnescapeTrust
Policies, contact, and answers
Clear pages for how Toolbench works, what we collect, and how to reach us.