EthicalFusion

JSON String Escaper

Escape text into a safe JSON string — quotes, newlines and special characters handled.

Was this tool helpful?

Thanks for the feedback!

The JSON String Escaper turns any text into a valid JSON string literal: it wraps it in double quotes and encodes the characters that JSON does not allow raw, such as quotes, backslashes, line breaks and tabs. Use it when you need to put multi-line text, a Windows path or a block of HTML into a JSON file or request body by hand.

Example

The two-line input

Line one
She said "hi"

becomes

"Line one\nShe said \"hi\""

The real line break turned into \n and each double quote gained a backslash. A Windows path works the same way: C:\Users\asha becomes "C:\\Users\\asha", because a single backslash starts an escape sequence in JSON.

Tabs become \t, and invisible control characters are written as Unicode escapes: a bell character (code 7) hidden in copied terminal output shows up as \u0007. That makes the escaper a handy way to spot stray characters that break a parser elsewhere.

The escape rules, per RFC 8259

CharacterEscaped as
Double quote\"
Backslash\\
Line feed / carriage return\n / \r
Tab\t
Backspace / form feed\b / \f
Other control characters\u0000 to \u001f

Everything else is left as it is. Forward slashes are not escaped, and Hindi text, emoji and ₹ stay as readable characters rather than \u codes, since JSON is UTF-8 and allows them directly. The escaping is done by the browser's built-in JSON.stringify, so the result is exactly what JavaScript itself would produce.

A common real case: keys and certificates

Service-account files and many cloud settings store a PEM private key or certificate inside a JSON value. The key has real line breaks, which are illegal inside a JSON string. Paste the whole block, from -----BEGIN PRIVATE KEY----- to the end line, and the tool gives one line with \n in each place a break was. Only do this with keys you are allowed to handle, and avoid pasting production secrets into any web page when a local script will do.

Mistakes to avoid

  • Doubled quotes. The output already includes the surrounding quotes. If you paste it into "message": "", remove the empty pair first, or you'll get ""text"", which is invalid.
  • Stray whitespace. The input isn't trimmed, so a trailing Enter becomes \n at the end of the value.
  • Escaping twice. Running already-escaped text through again turns \n into \\n, which shows up literally as backslash-n in your app.

A JSON string is also a valid JavaScript string literal, so the output can be pasted straight into code. To reverse the process, use the JSON String Unescaper, and check the finished document with the JSON Validator.

Frequently asked questions

Why does the escaped output have quotes around it?
The tool returns a complete JSON string literal, which includes its double quotes. Paste it as the whole value, not inside another pair of quotes.
Does it escape forward slashes?
No. JSON allows a plain / inside strings, so it is left unchanged.
Why are Hindi letters and emoji not turned into \u codes?
JSON text is UTF-8 and allows those characters directly, so escaping them is unnecessary. Only quotes, backslashes and control characters must be escaped.
How do I store a private key or certificate in JSON?
Escape the full PEM block so each line break becomes \n, then use the result as the value. Most libraries turn the \n back into real line breaks when reading it.
Is JSON escaping the same as JavaScript string escaping?
Nearly. A JSON string is a valid JavaScript string, but JavaScript also allows single quotes and a few extra escapes that JSON does not.

Related tools

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.