JSON Escape

Turn arbitrary text into a valid JSON string value.

Reverse
Input
TEXT input
Output
Result
Options

About this tool


JSON strings cannot contain a raw newline, tab, double quote or control character. Escaping converts each into its backslash sequence so the text becomes a legal JSON string value.

The most common need is embedding something multi-line into JSON by hand: a certificate, a private key, a SQL query or a log excerpt that has to become one field in a request body or a config file.

How to use it

  1. Paste or upload your textDrop a file onto the input pane, use the file picker, or paste the text directly.
  2. Adjust the options if neededThe defaults suit most input; open Options to change the behaviour.
  3. EscapePress Escape, or use Ctrl+Enter (Cmd+Enter on macOS).
  4. Copy or downloadCopy the result, or download it as a .txt file.

Worked examples


Each example below is executed against this tool by the test suite, so what you see is what the tool actually produces.

Multi-line text with a quote

Input

Line one
He said "hi"	done

Output

Line one\nHe said \"hi\"\tdone

The newline, quotes and tab all become escape sequences, so the text fits on one line as a JSON string.

What to watch for


The details that decide whether a conversion is correct, and where information can be lost without any error being raised.

What gets escaped
A double quote becomes \", a backslash becomes \\, and the control characters get their short forms: newline \n, carriage return \r, tab \t, backspace \b and form feed \f. Any other character below U+0020 becomes a \u00XX escape, because JSON forbids raw control characters in strings.
Escape the backslash first
Order matters. Escaping quotes before backslashes would turn an existing \" into \\", changing the meaning. Doing it in one pass, as here, avoids the double-escaping bug that hand-written escaping usually introduces.
Quotes around the result
By default you get just the escaped body, ready to paste between quotes you already have. Enable the wrap option to get a complete quoted string literal, which is what you want when building a JSON value from scratch.
Forward slashes need no escaping
JSON permits \/ but does not require it. Some older encoders escape every forward slash to avoid the sequence </script> appearing inside inline JSON in HTML. This tool leaves slashes alone, which is valid and more readable.

Limitations


  • Produces a JSON string body; it does not build a whole JSON document.
  • Processing happens in your browser, so very large inputs are bounded by available memory. Files above roughly 10 MB are handled but will feel slower, and multi-hundred-megabyte files are better suited to a command-line tool.

Questions


Should I include the surrounding quotes?
Enable "Wrap in quotes" if you need a complete JSON string literal. Leave it off when pasting into quotes that already exist in your document.
Why are my forward slashes unescaped?
JSON allows \/ but does not require it. Unescaped slashes are valid and easier to read. The escaped form only matters when embedding JSON inside an HTML <script> block.