JSON String Escape

Escape a string so it can sit inside JSON, or unescape a JSON string.

A string inside JSON, not a whole document

When you embed a sentence in JSON, quotes, backslashes and line breaks must be escaped. This page does that to the text you paste, or it undoes those escapes. It does not pretty-print an object. For that, use JSON formatter. The general escape string page is for source code in several languages, not specifically a JSON string.

How to use it

  1. Paste the raw text to escape, or an escaped JSON string to unescape.
  2. Choose Escape or Unescape.
  3. On escape, “Keep the surrounding quotes” wraps the result in double quotes so you can drop it into an object. Turn it off when you only want the inside.
  4. The example is the two characters quote-hi-quote, then a real line break. Escaped, the break becomes \n.

Which escapes you get

Double quotes and backslashes are escaped. Line breaks become \n, and a carriage return becomes \r. Characters outside the usual ASCII range can be left as themselves, because JSON allows UTF-8. Unescape reads a JSON string. If you leave the surrounding quotes on, they are part of the JSON string. If the text is not a valid JSON string, unescape fails instead of returning a partial line.

Where people use it

  • Putting a paragraph into a JSON fixture by hand.
  • Reading a log line where the message is still escaped.
  • Checking that a newline survived as \n and not as a broken line in the file.

Questions

I pasted a whole object and unescape failed.

Unescape expects one JSON string, not an object. Format the object on the JSON formatter.

Why are there quotes around the result?

“Keep the surrounding quotes” is on. Turn it off if you wanted only the escaped interior.