String Escape & Unescape

Escape and unescape strings for JSON, JavaScript, Java, C#, Python, SQL, MySQL, regex, the shell and CSV.

20 of up to 100,000 characters

Quotes ", \ and the control characters as \b, \f, \n, \r, \t or \uXXXX.

Result
It's \"ready\"\nship it

Runs in your browser. Nothing you enter is sent anywhere.

How to use it

  1. Paste into Text.
  2. Choose Escape to turn text into the inside of a string literal, or Unescape to turn a literal’s contents back into text.
  3. Pick the Language. The line under the menu sums up that language’s rule.

The Result updates as you type. Escaping gives you what goes between the quotes, and you add the quotes yourself, except for Shell and CSV, which quote for you. Unescape takes the same back: a literal’s contents without its quotes, or the whole quoted Shell or CSV value. A broken escape is reported with its position, counting from 0.

Examples

The page’s sample text, two lines with both kinds of quote:

It's "ready"
ship it

With JSON it becomes It's \"ready\"\nship it, for a double-quoted string. With Python it becomes It\'s "ready"\nship it, because the Python rules are for a single-quoted literal.

With Shell and Escape, it's $HOME becomes 'it'\''s $HOME'. Inside single quotes $HOME isn’t expanded. Unescape also reads the form Python’s shlex.quote writes, so 'it'"'"'s $HOME' comes back as it's $HOME.

A Windows path, C:\Users, with JSON and Unescape fails with:

Malformed escape at position 2 (counting from 0): \U is not a JSON escape.

With JavaScript the same text unescapes to C:Users, because JavaScript reads an unknown escape as the character itself. Either way, the path needs \\ to keep its backslash.

A CSV field holding a quote and a comma, 5" screen, black, escapes to "5"" screen, black".

SQL (standard) escapes It's in C:\temp to It''s in C:\temp, whose \t MySQL reads as a tab; SQL (MySQL) gives It''s in C:\\temp.

Good to know

  • JSON follows RFC 8259. A string must escape ", \ and the control characters U+0000 to U+001F. The tool writes \b, \f, \n, \r and \t for those that have a short form and \u001f style for the rest. Unescaping joins a surrogate pair such as \uD83D\uDE00 into one character.
  • JavaScript output works in single or double quotes. Both kinds of quote get a backslash. It isn’t meant for template literals, where a backtick or ${ would still need escaping.
  • Unescaping follows each language’s grammar. JavaScript accepts \u{1F600} and \x41, and rejects legacy octal escapes like \07, as strict-mode code does. Java accepts octal escapes up to \377 and \s for a space. Python accepts \x, \u, \U and octal, but not \N{…} names, and rejects unknown escapes like \q.
  • C#’s \x takes up to four hex digits. \x41BC is one character, 䆼, not A followed by BC. \u0041 always takes exactly four.
  • SQL (standard) doubles single quotes, and that’s all, the rule in PostgreSQL, SQL Server, SQLite and Oracle. SQL (MySQL) follows MySQL’s default, where backslashes are escapes: it also writes \ as \\, and NUL, line breaks and Ctrl-Z as \0, \n, \r and \Z. With NO_BACKSLASH_ESCAPES set, MySQL takes the standard form.
  • Shell escaping uses POSIX single quotes. Nothing inside them is special, so $, backticks and backslashes stay literal; only ' itself needs the '\'' dance. Unescape reads one shell word with single quotes, double quotes and backslashes, and rejects anything the shell would expand or split, like an unquoted space, $ or *.
  • CSV follows RFC 4180. A field is quoted only when it holds a comma, a double quote or a line break, and a quote inside it is doubled.
  • Regex escaping covers JavaScript’s syntax characters . * + ? ^ $ { } ( ) | [ ] \ plus /, so the result works in a regex literal, with or without the u and v flags. - is left alone, so use the result outside a character class.

Questions

How do I escape a string for JSON?

Paste it, choose Escape and JSON, and wrap the result in double quotes. In code, JSON.stringify(text) returns the escaped string with its quotes. Python’s json.dumps(text) does the same but also writes non-ASCII characters as \u escapes unless you pass ensure_ascii=False.

How do I unescape a JSON string?

Paste the string’s contents without the outer quotes and choose Unescape with JSON; in code, call JSON.parse on the quoted string. A log line showing {\"id\":7} holds JSON inside a JSON string; unescape it once to get the inner JSON.

How do I escape a single quote in SQL?

Double it: O'Brien is written 'O''Brien'. In MySQL, use SQL (MySQL). Better still, pass the value as a query parameter, such as ? or $1, so the database never parses it as SQL.

How do I put a single quote inside a single-quoted shell string?

You can’t escape it inside the quotes, so close them, add an escaped quote and reopen: 'it'\''s'. The shell joins the three pieces into one argument, it's. Double quotes work too, "it's", but there $ and backticks are still expanded.