Encode & Decode
How to Escape and Unescape Strings for JSON and JavaScript
Learn how backslash escaping works in JSON, JavaScript and other languages, and how to safely escape or unescape any string right in your browser.
Try the toolBackslash Escape / Unescape →When a stray quote breaks your JSON
You paste a sentence into a JSON config, hit save, and the parser explodes with Unexpected token. Nine times out of ten the cause is a character that has special meaning inside a string literal — a double quote, a literal line break, or a backslash — that was never escaped. Every language that uses quoted strings needs a way to say “treat this next character as data, not syntax,” and that mechanism is backslash escaping.
How backslash escaping works
Inside a quoted string, the backslash (\) is an escape character. It tells the parser that the character immediately after it should be interpreted literally or as a control code, not as the end of the string or as ordinary text. So \" means “a real double-quote character,” and \\ means “a single literal backslash.”
The most common escape sequences are shared across JSON, JavaScript, and many other languages:
\"— a double quote inside a double-quoted string\\— a literal backslash\n— newline (line feed)\t— tab\r— carriage return\uXXXX— a Unicode code point, e.g.éfor é
JSON keeps the list short and strict: those sequences plus \/, \b, and \f. JavaScript is looser — it also allows single-quoted and backtick strings, hex escapes like \x41, and template literals. That difference matters when you move text between a .json file and a .js source file.
A practical walkthrough
Say you have a raw multi-line string with quotes and a Windows path:
She said "hello"
Path: C:\Users\me
Dropped straight into JSON, that is invalid — the inner quotes close the string early and the backslashes start escape sequences. Escaped correctly, the same value becomes a single valid JSON string:
"She said \"hello\"\nPath: C:\\Users\\me"
Going the other direction, unescaping takes an escaped literal and gives you the human-readable original — handy when you pull a string out of a log file or an API response and want to read the real newlines and quotes. Paste the escaped value into the tool, click unescape, and you get the raw text back.
Common pitfalls
- Double-escaping. If you escape an already-escaped string,
\nbecomes\\nand stops being a newline. Escape once, at the boundary where the text enters the string. - Forgetting the backslash itself. Regex patterns and Windows paths are full of backslashes; each one needs to become
\\in JSON. - Assuming JSON and JavaScript are identical. A single-quoted string with
\x41is fine in JS but invalid in JSON. - Hand-escaping long blocks. Manually inserting backslashes across dozens of lines is where typos creep in — let a tool do the mechanical part.
Do it in your browser
The Backslash Escape / Unescape tool escapes and unescapes strings entirely in your browser — nothing is uploaded, there’s no sign-up, and it’s free. Paste your text, choose escape or unescape, and copy the result. Once your string is clean, drop it into the JSON Formatter to validate the surrounding document, or reach for URL Encode / Decode when the same value needs to travel safely inside a query string.
Conclusion
Escaping is a small detail with an outsized power to break a build. Understanding the handful of core sequences — and letting a tool handle the tedious, error-prone conversion — keeps your strings valid whether they live in JSON, JavaScript, or anywhere else quotes and backslashes collide.