What this tool does
A JSON string is text in double quotes where some characters are written with a backslash: a double quote becomes \", a backslash becomes \\, a line break becomes \n and a tab becomes \t. This tool takes your text and writes it as that kind of string. It is the same thing JSON.stringify does in JavaScript when you give it a string.
You need this when one JSON document has to carry another one, or any text with quotes and line breaks, inside a single field. Writing the escapes by hand is slow and easy to get wrong. One missed backslash makes the whole payload invalid.
How to use it
- Paste text or JSON into the left box, press Open file, or press Sample.
- The escaped string appears on the right as you type.
- Choose the options you need:
- Compact JSON first removes the formatting from valid JSON before escaping it. It is on by default.
- Wrap in quotes adds the outer double quotes. Turn it off if you are pasting into a place that already has quotes.
- Escape non-ASCII as \u writes accented letters, symbols and other non-ASCII characters as
\ucodes.
- Press Copy, or Download to save the result as a text file.
The line under the boxes tells you whether the input was valid JSON and how many characters the result has.
Examples
Plain text with a quote and a line break:
He said "hello"
then left.
becomes:
"He said \"hello\"\nthen left."
Formatted JSON with Compact JSON first on:
{
"name": "Ada",
"tags": ["admin", "dev"]
}
becomes:
"{\"name\":\"Ada\",\"tags\":[\"admin\",\"dev\"]}"
With Compact JSON first off, the indentation is kept and escaped, so { followed by a line break and "name": "Ada" turns into:
"{\n \"name\": \"Ada\"\n}"
A Windows path, where every backslash is doubled:
"C:\\Users\\Ada\\notes.txt"
Non-ASCII characters
JSON allows any Unicode character inside a string, so Café 東京 is valid as it is:
"Café 東京"
Some older systems, terminals or byte-limited fields only accept ASCII. With Escape non-ASCII as \u on, the same text becomes:
"Caf\u00e9 \u6771\u4eac"
Characters outside the Basic Multilingual Plane, such as most emoji, are written as two codes (a surrogate pair). A grinning face emoji becomes "\ud83d\ude00". Any JSON parser turns these back into the original characters.
Without the outer quotes
Turn off Wrap in quotes when the string goes into a template, a form field or an existing pair of quotes. The text Tab followed by a tab and here then comes out as:
Tab\there
How it works
The tool uses the browser’s own JSON.stringify, so the escaping follows the JSON standard exactly. Before that, if Compact JSON first is on and the input starts with { or [, it tries to read the input as JSON. If that works, the JSON is printed without whitespace, keeping numbers exactly as you wrote them. Then the whole text is escaped as one string. Nothing is sent to a server.
Tips and limits
- The result is a JSON string, not a JavaScript template literal or a shell string. Shells and some languages need their own quoting on top.
- If you stringify the result again, you get a double-escaped string. That is sometimes what an API expects, but usually it is a sign of a bug.
- Very large inputs are fine. The work is done locally and the speed depends on your device.
Other ways to do it
JavaScript. JSON.stringify(text) returns the escaped string. To embed an object, use JSON.stringify(JSON.stringify(obj)).
Python. json.dumps(text) does the same for a string. Pass ensure_ascii=False to keep non-ASCII characters as they are, since the default escapes them.
jq. jq -Rs . file.txt reads a whole file as raw text and prints it as one JSON string.
Related tools
To turn an escaped string back into readable text or JSON, use the JSON Unstringifier. To check or pretty print JSON first, use the JSON Formatter. To build JSON from a spreadsheet, use CSV to JSON.