JSON Escape and Unescape

Escape text into a JSON string, or unescape it back.

JSON Escape and Unescape tool

About the JSON Escape and Unescape tool

Putting an HTML snippet, a multi-line message or a Windows file path inside JSON breaks the document unless quotes, backslashes, new lines and tabs are escaped. This JSON escape tool converts any text into a valid JSON string literal and unescapes it again: a quotation mark becomes a backslash followed by the quote, a line break becomes backslash n, and a backslash is doubled. It saves hand-editing errors when building API requests, configuration values or test data, and the unescape mode makes log entries and stored strings readable again. For checking whole documents, use the JSON Formatter.

Escaping follows the JSON standard (RFC 8259): a double quote becomes \", a backslash \\, a new line \n, a carriage return \r, a tab \t, and other control characters \u0000-style codes. Unescaping parses the string with the browser's own JSON parser, so the result is exactly what a JSON library would read; if the input is not a valid JSON string, the error explains what is wrong. The forward slash may be escaped as \/ for embedding in HTML script tags.

Example: the two lines He said "hi" and Bye become "He said \"hi\"\nBye". Unescaping "C:\\Users\\Ana" gives C:\Users\Ana.

To check or format a whole JSON document use the JSON formatter, to sort its keys the JSON sorter, and for strings in URLs the URL encoder.

How to use the JSON Escape and Unescape tool

  1. Choose the directionEscape text into JSON, or unescape a JSON string.
  2. Paste the inputAny text, or an escaped string with or without quotes.
  3. Set the optionsSurrounding quotes, \u for non-ASCII, escaped slashes.
  4. Copy the resultPaste it into your JSON, code or config.

JSON Escape and Unescape features

  • RFC 8259 escaping
  • Quotes, backslashes, new lines and tabs
  • Control characters as \u codes
  • Optional \u escapes for non-ASCII
  • Optional escaped forward slashes
  • Unescape with the browser's JSON parser
  • Clear error messages

Common uses

  • Embedding HTML or text in JSON
  • Writing API request bodies
  • Reading escaped strings in logs
  • Config files and environment variables
  • Debugging serialization bugs

Escaping JSON in practice: HTML templates in API payloads, never building JSON by hand, JSON inside web pages, Urdu text, regex and private keys

By the FreeToolsWorld editorial team · Updated November 2026 · How we write and check our guides

Escaping is needed whenever text has to travel inside a JSON string: an HTML email template sent to an email API, a regular expression in a configuration file, a private key stored in an environment variable. This guide works through placing an HTML template in a payload, explains why JSON should never be built by joining strings, and covers JSON embedded in web pages, Urdu text, emoji, regular expressions, Windows paths and multi-line keys.

A worked example: an HTML template in a payload

An email service's API expects a JSON body with the message HTML in a field called html. The template contains a line such as:

  • <p class="note">Order #1042 is "ready"</p>

Escaped as a JSON string, it becomes "<p class=\"note\">Order #1042 is \"ready\"</p>", and the template's line breaks become \n. Pasting the escaped value into the html field produces a valid request, whereas pasting the raw template would end the string at the first quote and break the whole payload.

Never build JSON by joining strings

Code that assembles JSON by joining text, such as adding quotes around a customer's name, breaks the moment the name contains a quote or backslash. Worse, a carefully crafted value can inject extra fields into the request. Always build an object and let a serialiser produce the JSON, with JSON.stringify in JavaScript or json.dumps in Python, which escape everything correctly.

JSON inside web pages

Pages often embed data as JSON inside a script tag for scripts to read. If any string contains the text </script>, the browser ends the script block there, and the rest can be interpreted as HTML, opening the door to cross-site scripting. Escaping the less-than sign as \u003c when embedding JSON in HTML prevents this; many frameworks offer a helper that does it automatically.

Urdu text

JSON can carry Urdu directly as UTF-8, and the escaped form is optional. Python's json.dumps escapes non-ASCII characters by default, so پاکستان becomes a run of \u067e-style codes. Passing ensure_ascii=False keeps the Urdu readable in files and logs, which makes translation files far easier to review.

Emoji

When non-ASCII characters are escaped, emoji outside the basic range become two escape codes, called a surrogate pair. A grinning face becomes \ud83d\ude00. Both codes belong together, and splitting a string between them, for example when truncating text to a character limit, produces a broken character. Truncate before escaping, using functions that count whole characters.

Regular expressions in configuration

A validation pattern such as ^03\d{9}$ for Pakistani mobile numbers must have its backslash doubled inside JSON, becoming "^03\\d{9}$". Forgetting this either breaks the JSON or silently changes the pattern, because \d is not a valid JSON escape. When a pattern works in a regex tester but fails from configuration, missing doubled backslashes are the usual cause.

Windows paths

Windows paths in JSON settings files need doubled backslashes too, such as "C:\\Users\\Ayesha\\Documents". Many Windows programs also accept forward slashes, as in C:/Users/Ayesha/Documents, which avoids escaping altogether. Check the program's documentation before relying on that shortcut.

Private keys in environment variables

Service account credentials and private keys contain line breaks, and storing them in a single-line environment variable often turns each line break into the two characters backslash and n. Code then has to convert them back, typically by replacing the escaped sequence with a real newline before using the key. Signature errors with otherwise correct keys frequently come from this step being skipped.

Logging escaped values

Logs that store JSON strings show escapes literally, which makes multi-line messages hard to read. Unescaping a copied value turns it back into the original text with its line breaks and quotes. Remember that logs may contain personal data or secrets, so handle copies with the same care as the originals.

Common mistakes

  • Building JSON by concatenating strings.
  • Embedding JSON in a script tag without escaping less-than signs.
  • Forgetting to double backslashes in regular expressions and paths.
  • Truncating escaped text in the middle of a surrogate pair.

Related tools

To escape text for HTML instead, use HTML Encoder & Decoder. To check a pattern before escaping it, try Regex Tester, and to inspect the finished payload, JSON Viewer.

Apostrophes and single quotes

JSON strings must be wrapped in double quotes; single-quoted strings are valid in JavaScript but not in JSON. Apostrophes inside a JSON string need no escaping at all, so "Ayesha's order" is perfectly valid. Escaping an apostrophe with a backslash is actually an error in strict JSON parsers.

Payloads on the command line

Sending JSON with curl gets awkward because the shell has its own quoting rules. In bash, wrapping the whole payload in single quotes usually works, but the Windows command prompt needs different escaping. Saving the payload to a file and sending it with -d @payload.json avoids shell quoting entirely and works the same everywhere.

Privacy

Escaping and unescaping run in your browser. Your text is not uploaded or stored.

Frequently asked questions

How do I escape a string for JSON?

Replace " with \", \ with \\, new lines with \n, carriage returns with \r and tabs with \t, and wrap the result in double quotes. This tool does all of that in one step.

How do I unescape a JSON string?

Choose unescape and paste the string, with or without its surrounding quotes. Escape sequences such as \n and \" are turned back into new lines and quotes.

Which characters must be escaped in JSON?

The double quote, the backslash and control characters below U+0020, including new lines and tabs. Everything else, including emoji and non-English letters, may appear as is.

Do I need to escape forward slashes?

No, JSON allows / as is. Some systems escape it as \/ so that the text </script> cannot end an HTML script block early; turn on the option if you need that.

What does \u mean in JSON?

A Unicode escape: \u followed by four hexadecimal digits for a character code, such as \u00e9 for é. Characters outside the basic plane use two codes, a surrogate pair.

Why does my JSON say unexpected token?

Often because a string contains an unescaped quote or a raw line break. Escape the text with this tool and paste it back, or validate the whole document with the JSON formatter.

Is JSON escaping the same as JavaScript escaping?

Very similar, but JSON is stricter: it allows only double-quoted strings and a fixed set of escapes. A JSON string is always a valid JavaScript string, but not every JavaScript string is valid JSON.

Is my data sent anywhere?

No. The escaping and unescaping run in your browser, so tokens, keys and messages you paste stay on your device. That makes it safe for API keys and private messages, though you should still avoid pasting secrets into shared screens.

How do I escape JSON in JavaScript or Python?

In JavaScript, JSON.stringify(text) returns an escaped, quoted string. In Python, json.dumps(text) does the same; add ensure_ascii=False to keep non-English characters as they are.

Why does my JSON have \\n instead of new lines?

Because the string was escaped twice, once by a library and once more by hand or by a second serializer. Unescape it once here to see the real text, then fix the double encoding.