String Escaper

Escape a string for JSON or JavaScript — wrap it in quotes and escape the special characters — right in your browser. Paste raw text, get a ready-to-embed string literal.

Developer Tools Free No upload Instant

Loading String Escaper…

Your browser is preparing the tool. It runs 100% locally.

Quick Answer

Paste raw text and the tool returns it as an escaped string literal — wrapped in double quotes, with the characters that would otherwise break the string escaped: a quote becomes backslash-quote, a backslash is doubled, a newline becomes backslash-n, and so on. It produces a JSON string, which is also valid in JavaScript, ready to paste into code or a config. It runs in your browser; nothing is uploaded.

What the String Escaper Does

This tool takes any text and turns it into a safe, quoted string literal you can drop straight into JSON or JavaScript source. It wraps the text in double quotes and escapes the characters that would otherwise end the string early or be invalid, such as quotes, backslashes, newlines and tabs.

It is the quick way to embed an arbitrary value — a snippet of text, a path, a multi-line message — into code or a config file without hunting down every character that needs a backslash in front of it.

How It Works

The tool serialises your text as a JSON string. That means it surrounds the whole value in double quotes and replaces each special character with its escape sequence: a double quote becomes a backslash followed by a quote, a literal backslash is doubled, and control characters like newline and tab become their backslash forms.

The result is a complete string literal, not just the inner text. Because JSON string syntax is a subset of JavaScript string syntax, the same output works in both — you can paste it wherever a double-quoted string is expected.

Data source & conversion logic

  1. Take the raw text. Read the exact text you paste, including any quotes, backslashes and line breaks.
  2. Escape special characters. Replace quotes, backslashes and control characters with their backslash escape sequences.
  3. Wrap in quotes. Surround the escaped text in double quotes to form a complete string literal.
  4. Return the literal. Output the ready-to-embed JSON or JavaScript string, including its quotes.

What gets escaped

wrap the text in double quotes, and inside it: • a double quote → backslash + quote • a backslash → two backslashes • a newline → backslash n · a tab → backslash t • other control characters → backslash u escapes
Worked example
Before: He said "hi" (followed by a new line, then) Line two After: "He said \"hi\"\nLine two"

The output includes the surrounding quotes — it is a complete string literal, not just the escaped contents. It escapes for JSON and JavaScript, not for HTML, URLs, regular expressions or shells.

Privacy

Your text never leaves your device. It is escaped in your browser with no network request, so nothing is uploaded.

Because the work is local, you can safely escape text that contains tokens or private data without it being stored or sent anywhere.

Nothing persists between sessions — close the tab and the input and output are gone.

Standards & references

  • JSON strings (RFC 8259) — JSON defines exactly which characters must be escaped inside a double-quoted string and how. The tool produces a string that follows this grammar, which is why the output is always valid JSON.
  • JavaScript string literals — JSON string syntax is a subset of JavaScript's, so a JSON string literal is also a valid JavaScript double-quoted string. The same output works in both.
  • Context-specific escaping — Escaping is per-context. A JSON or JavaScript string escape is not the same as escaping for HTML, a URL, a regular expression, CSV or a shell — each needs its own rules.

Accuracy & Limitations

The output is a valid JSON string literal — the same result as a standard serialise step — so it is reliable for embedding text in JSON or JavaScript. The quotes are part of the output, because it produces a complete literal.

It escapes for one context: JSON and JavaScript strings. It is not the right tool for escaping into HTML, a URL, a regular expression, CSV or a shell command — each of those has different rules, and using a JSON escape there would be wrong.

Non-ASCII characters are kept as themselves rather than turned into backslash-u escapes, since they are valid in a JSON string as UTF-8. If you specifically need an ASCII-only literal, a different setting or tool would escape those too.

It produces a double-quoted literal. If your target needs single quotes, or a different quoting style, you would adjust the output by hand — the escaping inside would still be correct for double quotes.

Real-World Use Cases

Embedding text in JSON

Turn a multi-line or quote-containing value into a safe JSON string.

Inlining a string in code

Paste an arbitrary value into JavaScript without manual escaping.

Escaping a file path

Double the backslashes in a path so it is a valid string literal.

Handling multi-line text

Convert line breaks into backslash-n for a single-line literal.

When to use it — and when not to

Good for

  • Escaping text for JSON or JavaScript
  • Embedding a string with quotes or newlines
  • Doubling backslashes in a path
  • Turning multi-line text into one literal

Not the best choice for

  • Escaping into HTML (use HTML entity escaping)
  • Escaping into a URL (use percent-encoding)
  • Regular-expression or shell escaping
  • Producing a single-quoted literal directly

For HTML, use an HTML encoder; for URLs, a URL encoder; each context has its own escaping. For a single-quoted target, take this output and adjust the quote style by hand — the inner escaping stays valid.

Frequently Asked Questions

What does the string escaper do?
It turns raw text into an escaped string literal: it wraps the text in double quotes and escapes the characters that would otherwise break the string, such as quotes, backslashes, newlines and tabs. The result is valid JSON and JavaScript.
Does the output include the quotes?
Yes. It produces a complete string literal, surrounding quotes included, so you can paste it directly where a string is expected. It is not just the escaped inner text.
Is this for JSON or JavaScript?
Both. It produces a JSON string, and because JSON string syntax is a subset of JavaScript's, the same output is also a valid JavaScript double-quoted string.
Can I use it to escape for HTML or a URL?
No. Escaping is context-specific. For HTML use an HTML encoder, and for URLs use percent-encoding. A JSON string escape is wrong for those contexts.
What happens to my newlines?
Each newline becomes a backslash-n escape, so multi-line text collapses into a single-line literal that still represents the same characters. Tabs similarly become backslash-t.
Does it escape non-ASCII characters?
No. Accented letters, emoji and other non-ASCII characters are valid inside a JSON string as UTF-8, so they are kept as themselves rather than turned into backslash-u escapes.
Why are my backslashes doubled?
A single backslash starts an escape sequence, so to represent a literal backslash the tool doubles it. That is why a Windows path comes out with two backslashes at each separator.
Is this the same as encoding or encryption?
No. Escaping just makes text safe to embed in a string literal. It does not encode to another format like Base64, and it is not encryption — the text is fully readable in the output.
Can I get a single-quoted literal instead?
The tool produces a double-quoted literal. You can switch the outer quotes to single by hand if your target needs them; the inner escaping remains valid for the text.
Is my text uploaded anywhere?
No. The escaping happens entirely in your browser. Nothing is sent to a server, so text with secrets stays on your device.
How do I reverse it?
Parsing the literal as JSON, or using it as a string in code, gives back the original text. The escaping is fully reversible — it represents the same characters.
Why did my quote become backslash-quote?
An unescaped double quote would end the string early, so each double quote inside the text is escaped with a backslash. That keeps the whole value as one valid string.

References

Escapes by producing a JSON string literal (the same as JSON.stringify), so the output is valid for JSON and JavaScript; honest that it does not escape for other contexts like HTML, URLs or shells.

PopularHot

JSON Formatter

JSON Formatter for developers — instant, offline-style and privacy-safe.

DeveloperOpen Tool
Popular

JSON Validator

JSON Validator for developers — instant, offline-style and privacy-safe.

DeveloperOpen Tool
Popular

Base64 Encoder

Base64 Encoder for developers — instant, offline-style and privacy-safe.

DeveloperOpen Tool
Popular

Hash Generator

Fast, free Hash Generator that runs entirely client-side in your browser.

DeveloperOpen Tool
Popular

UUID Generator

Fast, free UUID Generator that runs entirely client-side in your browser.

DeveloperOpen Tool
Trending

CSS Minifier

Fast, free CSS Minifier that runs entirely client-side in your browser.

DeveloperOpen Tool

Ready to try the String Escaper?

It is free, private and runs entirely in your browser — no sign-up, no uploads, no limits.