Skip to content

String Escape & Unescape — JSON, JavaScript, Java, Python, SQL, CSV

Escape text for a JSON, JavaScript, Java, C#, Python, SQL, CSV, HTML or regex string, or decode escaped text back, with clear errors.

Escapes " \ and control characters (\n \t \uXXXX), as JSON.stringify does. Also escapes U+2028/U+2029 so the result is safe in old JavaScript.

47 characters
Done.
53 characters

Runs entirely in your browser. Nothing you paste is uploaded. Browse all free tools.

Found a problem, or something that could be better? We read every message and we fix things quickly.

This string escape and unescape tool converts text into a valid string literal for JSON, JavaScript, Java, C#, Python, SQL, CSV, HTML or a regular expression, and turns an escaped string back into the text it stands for. Paste a log line full of \n and \" and read it, or paste a paragraph with quotes, backslashes and emoji and get a string you can drop into code without a syntax error.

Each format has its own rules, and the tool follows them instead of adding backslashes everywhere: standard SQL only doubles single quotes, MySQL also escapes backslashes, CSV wraps the field in quotes and doubles the ones inside, C# reads \x41BC as one character, and Python keeps the backslash in an unknown escape like \d. When unescaping, malformed input (an unfinished \u12, a stray quote, a lone backslash) is reported with the character position rather than silently mangled. It does not evaluate code, so pasting a string is always safe.

How to use

  1. Choose Escape or Unescape.
  2. Pick the format: JSON, JavaScript, Java / C#, Python, SQL, MySQL, CSV, HTML text or attribute, or regex.
  3. Paste your text. The result updates as you type.
  4. Set the options you need: quote character, ASCII-only output, or add / strip the surrounding quotes.
  5. Copy the result, or press "Use output as input" to check the round trip.

Frequently asked questions

How do I escape a string for JSON?

Inside a JSON string, the double quote and the backslash must be written as \" and \\, and control characters as \n, \r, \t, \b, \f or \uXXXX. Everything else, including accented letters and emoji, may appear as is. Choose JSON string and Escape: the output is exactly what JSON.stringify produces, without the outer quotes unless you ask for them. Tick ASCII only to write every non-ASCII character as \uXXXX, with emoji as a surrogate pair such as 😀.

How do I unescape a JSON string with \n and \" in it?

Choose JSON string and Unescape, then paste the text. \n becomes a real line break, \" a quote, \uXXXX the character it codes, and a pair like 😀 one emoji. If you copied the value with its quotes, tick "Input has surrounding quotes". If it was escaped twice (\\n), press "Use output as input" and unescape again.

How do you escape a single quote in SQL?

Write it twice: 'O''Brien'. That is the only escape standard SQL has, and it works in PostgreSQL, SQLite, SQL Server, Oracle and MySQL. MySQL in its default mode also treats the backslash as an escape, so a Windows path like C:\new must be written 'C:\\new' there. Choose MySQL / MariaDB for that. For values that come from users, use parameterized queries rather than escaping by hand.

What is the difference between escaping for JavaScript and for JSON?

JSON allows only double-quoted strings and a short list of escapes; \x41, \v, \' and \u{1F600} are all invalid JSON. JavaScript accepts all of them, plus single quotes and backtick template literals, where ${ must be escaped as \${ or it starts an expression. Valid JSON escapes are valid JavaScript, but not the other way round.

How do I escape special characters in a regular expression?

Put a backslash before each character that has a meaning in a pattern: . * + ? ^ $ ( ) [ ] { } | / and \ itself. Choose Regular expression and Escape: "1.5*(x)" becomes 1\.5\*\(x\), which matches only that literal text. The output is valid in JavaScript (including the u flag), Python, Java, PCRE and .NET.

Is my text sent to a server?

No. Escaping and unescaping happen in your browser with plain JavaScript. Nothing you paste is uploaded or stored.

Escape sequences by language

• JSON: \" \\ \/ \b \f \n \r \t and \uXXXX. Nothing else. Raw control characters are not allowed inside a string.

• JavaScript: everything JSON has, plus \' \` \v \0 \xNN, \u{...} for any code point, and a backslash before a line break to continue the string.

• Java: \b \t \n \f \r \s \" \' \\, octal \0 to \377, and \uXXXX, which Java decodes before it even parses the code.

• C#: the Java set without octal, plus \a \v \0, \xH to \xHHHH (one to four digits) and \UXXXXXXXX.

• Python: \\ \' \" \a \b \f \n \r \t \v, octal \ooo, \xhh, \uXXXX, \UXXXXXXXX and \N{name}. Unknown escapes such as \d keep their backslash.

• SQL: no backslash escapes in standard SQL; a quote inside '...' is written ''. MySQL adds \0 \' \" \b \n \r \t \Z \\.

• CSV (RFC 4180): wrap the field in double quotes when it contains the delimiter, a quote or a line break, and write each quote inside as "".

Common escaping mistakes

• Double escaping: a value escaped twice shows \\n instead of a line break. Unescape it twice.

• Windows paths in code: "C:\new\table" contains a newline and a tab in JavaScript, Java and C#. Escape the backslashes or use a raw or verbatim string (r"..." in Python, @"..." in C#).

• Escaping instead of parameterizing: escaping SQL by hand is easy to get wrong across dialects. Use parameters for user input and keep escaping for scripts and fixtures.

• Mixing up HTML and URL encoding: & is for HTML text, %26 is for URLs. Use the HTML entity encoder or the URL encoder for those.