Skip to content
JSON Tools

Last updated:

Escape Text for JSON

Escape text for JSON in seconds. Paste your text and this free JSON escape tool adds backslashes to quotes, line breaks and other special characters as you type, so the result is safe to use inside a JSON string. Everything runs in your browser.

Plain text

0 characters · 0 lines

Escaped for JSON

Loading tool
0 characters · 0 linesLive preview

Your text is processed in your browser and never uploaded.

More free tools you may need next.

How to use JSON Escape

  1. 1

    Paste or type your text

    Drop your text into the left box. It can be a sentence, a multi-line message, a SQL query or a Windows file path. Not sure what to try? Press Example and watch what happens to the quotes and the line break.

  2. 2

    Check the result

    The escaped version appears on the right as you type, so there is nothing to click or submit. If you paste a very large file, the tool switches to large text mode and still converts all of it.

  3. 3

    Copy it into your JSON

    Press Copy and paste the result between the double quotes of your JSON string. The tool doesn't add the outer quotes, so you won't end up with a doubled pair.

What does it mean to escape text for JSON?

Escaping text for JSON means rewriting the few characters that would otherwise break a JSON string. A JSON string starts and ends with a double quote, so a quote in the middle of your text looks like the end of the string and the parser gets confused. A raw line break causes the same trouble, because JSON doesn't allow one inside a string.

The fix is a backslash. A quote becomes \", a line break becomes \n and a backslash itself becomes \\. Every JSON parser reads these sequences and gives back exactly the text you started with, so escaping is simply a safe way to carry that text inside a string.

It isn't encryption and it isn't compression. Nothing is hidden and nothing is lost.

Which characters need escaping?

The JSON specification (RFC 8259) is short on this point. Inside a string, three kinds of character must be escaped:

  • The double quote ", written as \"
  • The backslash \, written as \\
  • Control characters below U+0020, such as the line feed \n, the carriage return \r and the tab \t. A control character without a short form is written as a Unicode escape, for example \u0001

Everything else can stay as it is. Accented letters, Urdu and Arabic text, Chinese characters and emoji are all valid inside a UTF-8 JSON string, and this tool leaves them alone. Single quotes never need escaping, because JSON only uses double quotes. The forward slash may be written as \/, but you don't have to.

A worked example

Say you want to send this message through an API:

Text
She said "hello"
C:\Users\Sam

Pasted straight into JSON, it fails twice over: the quotes end the string early, and the line break and backslashes aren't valid. Escaped, it becomes one safe line:

JSON
{"message": "She said \"hello\"\nC:\\Users\\Sam"}

When this comes in handy

Most people land here in the middle of a practical job, like:

  • Writing a JSON request body by hand for a curl command or an API client.
  • Putting a multi-line message, an HTML snippet or a SQL query into a config file.
  • Preparing test data that contains quotes, apostrophes or file paths.
  • Pasting a stack trace or a log line into a JSON payload.
  • Adding examples that contain JSON strings to API documentation.

Escaping in your own code

If you're doing this inside a program, don't write the replacements yourself. Every major language has a serializer that does it correctly. In JavaScript, JSON.stringify returns the escaped string wrapped in quotes, so slice the quotes off if you only want the inside:

JavaScript
const text = 'She said "hello"\nC:\\Users\\Sam';
const escaped = JSON.stringify(text).slice(1, -1);
Python
import json

escaped = json.dumps(text)[1:-1]

Mistakes that break JSON

A few slips cause most of the 'unexpected token' errors people hit after escaping:

  • Escaping the whole document instead of one value. Run an entire JSON object through an escape tool and you get a string, not an object. Escape only the text that goes inside a string value.
  • Escaping twice. If you see doubled backslashes where you expected single ones, the text was already escaped once. Use JSON Unescape to peel a layer off.
  • Forgetting the backslashes in Windows paths. C:\Users has to be written C:\\Users inside JSON.
  • Adding quotes twice. This tool leaves the outer quotes off, so add them yourself, once.
  • Wrapping a string in single quotes. JSON strings only use double quotes. Single quotes are fine inside the text, but they can't wrap it.

Is it safe to paste private text here?

Everything happens in your browser. The text you paste is processed on your own device with JavaScript and isn't sent to our servers, so API payloads and internal messages stay with you. The Privacy Policy covers the details.

Frequently asked questions

Paste your text into the left box. The tool replaces quotes, backslashes, line breaks and other control characters with their JSON escape sequences straight away. Copy the result and put it between the double quotes of your JSON string.

Three kinds: the double quote, the backslash and the control characters from U+0000 to U+001F, which includes line breaks and tabs. Everything else is allowed as it is.

No. You get only the escaped content, so you can paste it into a pair of quotes you already have. If you need a complete JSON string, wrap the result in double quotes yourself.

No. Writing \/ is allowed but optional. Some developers do it when they embed JSON in an HTML script tag, to avoid an accidental closing tag. This tool leaves the slash unchanged.

JSON strings are always wrapped in double quotes, so a single quote inside one is an ordinary character. It only needs escaping in other languages, for example inside a JavaScript string that is itself wrapped in single quotes.

No. Urdu, Arabic, accented letters, Chinese characters and emoji stay readable, because they are valid in UTF-8 JSON. If you need ASCII-only output, you would have to convert them to Unicode escapes separately. This tool doesn't do that.

No. The conversion runs in your browser and the text never leaves your device.

Use the swap button between the two boxes. It opens JSON Unescape with your result already loaded, or you can paste escaped text into that tool directly.