String Escaping in JSON, Python, SQL, Shell and More
How quotes, backslashes, line breaks and Unicode are escaped in JSON, JavaScript, Python, Go, Rust, SQL, shell and HTML, plus raw strings and round trips.

String Escaping in JSON, Python, SQL, Shell and More
Putting text inside a string literal sounds trivial until the text contains a quote, a backslash, a line break or an emoji. Every language has its own escaping rules, and the rules differ in small ways that cause real bugs: a Windows path that loses its backslashes, a SQL query that breaks on a surname with an apostrophe, a shell command that splits on a space. The free String Escaper converts any text into a correct literal for twenty languages and contexts, decodes literals back into text, and checks that the round trip loses nothing.
The common ground
Most C-family languages share a core set of escapes: backslash-n for a line feed, backslash-t for a tab, backslash-backslash for a backslash, and backslash-quote for the quote character that delimits the string. Beyond that, they diverge:
- JSON requires double quotes, escapes control characters, and represents other characters either directly or as backslash-u with four hex digits. Characters outside the Basic Multilingual Plane, such as most emoji, become a surrogate pair.
- JavaScript follows JSON but also allows single quotes and template literals, where the backtick and the dollar-brace sequence need escaping.
- Python has the same core escapes, plus raw strings prefixed with r, where backslashes are literal. Raw strings are ideal for regular expressions and Windows paths, but cannot end with a single backslash.
- Go uses double-quoted strings with escapes, or backtick raw strings, which cannot contain a backtick.
- Rust has escapes with braces for Unicode and raw strings with hash marks, which can hold almost anything.
- C# and Java escape like C, with Unicode escapes for non-ASCII characters when you want pure ASCII source.
SQL is different
SQL string literals do not use backslash escapes in the standard. A single quote inside a string is written as two single quotes: O''Brien. MySQL also accepts backslash escapes by default, which is why MySQL and PostgreSQL literals are offered separately. SQL Server uses N-prefixed literals for Unicode text.
Escaping is useful for writing fixtures, migrations and one-off queries. In application code, always use parameterised queries instead of building SQL with escaped strings. Parameters remove the risk of SQL injection entirely.
Shell quoting
In POSIX shells, single quotes are the safest: everything inside is literal. The only character that cannot appear is the single quote itself, which is written by closing the quote, adding an escaped quote and opening a new one. The escaper produces this form, so filenames with spaces, dollar signs and quotes are passed safely as one argument.
HTML, CSS and regular expressions
HTML text needs ampersands, angle brackets and quotes turned into character references. CSS strings use backslash followed by a hex code point. Regular expressions need their special characters, such as dot, star, plus and brackets, escaped with a backslash to match them literally. Each of these is a separate context with its own rules, which is why using the right one matters.
The round-trip check
After escaping, the tool decodes the result with the same language's rules and compares it with your original text. A green check means the literal will produce exactly the text you started with. This catches subtle problems such as characters that need Unicode escapes or a raw string that cannot represent a trailing backslash.
Step by step
- Paste the text you want to put in a string.
- Choose escape or unescape.
- Pick the language or context.
- Check the round-trip result.
- Copy the literal into your code, or show every language at once to compare.
Privacy
Escaping runs in your browser, so passwords, keys and personal text used in test fixtures never leave your device.



