JSON Wallet

Escaping and Stringifying JSON Explained

8 min read

← All guides

"Escaping" and "stringifying" sound similar and are easy to confuse, but they solve different problems. Both come up constantly when you embed JSON inside another string — in a database column, a log line, an API field, or source code. Let's untangle them with concrete before-and-after examples.

Stringify: object to string

To stringify JSON means to turn a JSON value (an object or array) into a single string. In JavaScript this is what JSON.stringify() does. The result is text you can store or transmit.

// before (a JSON object)
{ "name": "Ada", "id": 1 }

// after stringify (a JSON string)
"{\"name\":\"Ada\",\"id\":1}"

Notice how the quotes inside got backslash-escaped — that is required, because the whole thing is now itself a string wrapped in quotes. This is why stringified JSON looks so noisy.

Unstringify: string back to object

Unstringify (or parse) is the reverse: it takes a JSON string and turns it back into a real, readable JSON object. This is what JSON.parse() does. You will use it when an API returns escaped JSON inside a string field and you need to read what's inside.

The double-encoding trap

A very common bug is JSON that has been stringified twice. Each round of stringification adds another layer of escaping, so you end up with a forest of backslashes and have to parse it twice to recover the object:

// stringified twice
"\"{\\\"name\\\":\\\"Ada\\\"}\""

// after first unstringify (still a string!)
"{\"name\":\"Ada\"}"

// after second unstringify (finally an object)
{ "name": "Ada" }

If parsing once gives you a string instead of an object, you are almost certainly looking at double-encoded data. Unstringify again.

Escape: make a string safe to embed

To escape means to add backslashes in front of special characters (double quotes, backslashes, newlines) so the text can sit safely inside another string. For example, before putting a snippet into a SQL query or a string literal:

// before
He said "hello" and left.

// after escaping
He said \"hello\" and left.

Unescape: remove the backslashes

Unescape is the reverse of escape: it strips the backslashes to give you the original, human-readable text. This is handy when you copy an escaped value out of a log file or a database and want to read it normally.

When do I need each one?

  • Stringify — before storing a JSON object in a single text field, cache, or message queue.
  • Unstringify — when a response gives you JSON-inside-a-string and you need the real object.
  • Escape — before embedding arbitrary text inside a JSON string, SQL statement, or code literal.
  • Unescape — to read an escaped value that was copied out of logs or a database.

Frequently asked questions

What is the difference between escaping and stringifying?

Stringifying converts a whole object into a string (and escapes the quotes inside as a side effect). Escaping just adds backslashes to special characters in a piece of text. Stringify operates on JSON values; escape operates on raw text.

Why does my JSON have so many backslashes?

Each layer of stringification escapes the previous layer's quotes. A pile of backslashes (\\\\\\") usually means the data was stringified more than once. Unstringify repeatedly until you get a real object.

Does stringifying change the data?

No — it only changes the representation. Parsing the string back gives you the exact same object. The data is preserved; only the wrapping changes.

Do it without the headache

Doing this by hand is fiddly and error-prone. In the JSON Wallet editor, the Stringify, Unstringify, Escape and Unescape buttons handle all the backslash bookkeeping for you. Paste your value, click the operation you need, and copy the result. If your input is malformed to begin with, the Repair tool can clean it up first — see common JSON errors.


Keep reading

Ready to put this into practice? Open the free JSON editor →