Skip to content
Case Converter Online
en

JSON Stringify Text

Paste any text or JSON and get it back as one escaped JSON string, ready to put inside another JSON document, a config value or a line of code.

Paste text or JSON, or press Sample. It all runs in your browser.

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

  1. Paste text or JSON into the left box, press Open file, or press Sample.
  2. The escaped string appears on the right as you type.
  3. 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 \u codes.
  4. 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.

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.

JSON Stringify: questions and answers

What does JSON stringify mean?

It means writing a value as JSON text. For a piece of text, that gives a JSON string: the text wrapped in double quotes, with quotes, backslashes, line breaks and tabs escaped so the result is valid JSON.

Why would I put JSON inside a JSON string?

Some systems store or send a whole JSON document as a single string field. Examples are message queues, webhook payloads, log lines, environment variables and some database columns. The inner JSON has to be escaped to fit.

What does Compact JSON first do?

If your input is valid JSON, its spaces and line breaks are removed before escaping. The result is shorter and has no \n escapes from the formatting. If the input is not valid JSON, the text is escaped exactly as typed.

How do I reverse it?

Use the JSON Unstringifier. It takes an escaped string, with or without the outer quotes, and gives back the original text or JSON.

More code and data tools

See all tools