How to Escape and Unescape JSON Strings
Sooner or later you have to put JSON inside JSON — store a payload as a string field, drop a config blob into an environment variable, or log a structured value on one line. Doing that safely means escaping the string so its quotes and newlines don't break the document around it. This guide explains what escaping does, when you need it, and how to unescape a value to read it back.
What does escaping a JSON string mean?
Escaping (also called stringifying) turns a piece of text into a single valid JSON string. It wraps the text in double quotes and replaces the characters JSON can't contain literally with escape sequences — a double quote becomes \", a newline becomes \n, a tab becomes \t. The result is one string value that can sit safely inside another JSON document or a source-code literal.
Which characters get escaped
The characters that must be escaped inside a JSON string are well defined:
- Double quote (") becomes \".
- Backslash (\) becomes \\.
- Newline, carriage return, and tab become \n, \r, and \t.
- Other control characters become \u escapes.
When you need to escape JSON
Escaping comes up whenever JSON has to live inside something that is also text:
- Embedding a JSON payload as a string field within another JSON document.
- Pasting JSON into a string literal in your source code.
- Storing JSON in an environment variable or a single config value.
- Writing a structured value to a log on one line.
Stringify, with or without the quotes
"Stringify" and "escape" describe the same operation. The only choice is whether to keep the surrounding double quotes. Keep them when you want a complete, ready-to-paste JSON string; drop them when you only need the escaped body to place between quotes you've already written.
Unescaping: reading a stringified value
The reverse is just as common. When you find a value that was stored escaped — a stringified payload in a log line, a serialized field in a database, a response wrapped in quotes — unescaping decodes every escape sequence back to the original, readable text. Then you can format or inspect it like any other JSON.
One rule of thumb: never escape JSON by hand with find-and-replace. It's easy to miss a backslash or double-escape a quote. Let JSON.stringify or a dedicated tool do it so the result is correct by construction.
Try it yourself
Escape any text into a JSON string — or unescape an escaped string back to raw text.