Saltar al contenido principal

Serializar JSON

JSON Stringify turns a JSON document into a JSON string literal — the escaped, quoted form you need to embed one payload inside another. It escapes quotes, backslashes and control characters in a single pass, so the double-escaping that chained find-and-replace produces cannot happen.

Solo local

La interfaz de esta herramienta está en inglés.

JSON document
0 B1 lineLn 1, Col 1
Escaped string literalread-only
0 B1 line

La guía de abajo solo está disponible en inglés.

How does JSON Stringify work?

This is the operation behind a recurring, unglamorous job: putting one payload inside another. A request body inside a webhook definition. A fixture inside a test file. A configuration document inside a Kubernetes ConfigMap value. A sample response inside an OpenAPI example field. In each case the inner document has to become a string in the outer one, which means every quote, backslash and newline inside it needs escaping.

People do this by hand with find-and-replace, and it goes wrong in a specific, predictable way. If you replace the quotes first and the backslashes second, the backslash you just introduced in front of each quote gets escaped a second time: you wanted \" and you have \\", which parses as a literal backslash followed by the end of the string. The document appears to work until something in it contains a quote.

Escaping in a single character scan makes that impossible. Each character is examined once and mapped once — " to \", \ to \\, newline to \n, tab to \t — with no opportunity for a later pass to re-process an earlier pass’s output.

RFC 8259 requires every control character below U+0020 to be escaped, so anything without a shorthand becomes a \u sequence. Non-ASCII characters are left alone by default, because modern JSON is UTF-8 and escaping them only makes the result larger and less readable. When a target genuinely needs pure ASCII — an older log pipeline, a system that mangles high bytes — the option escapes them, and correctly emits a surrogate pair for anything outside the Basic Multilingual Plane, since a single \u cannot represent an emoji and emitting one truncates it.

By default the input is validated as JSON first. Escaping something broken produces a string literal that is itself valid but whose contents will fail to parse later, somewhere much less convenient. Switch that off to escape arbitrary text, which is useful for log lines and shell snippets.

Document

{"name":"Ada","note":"line one
line two"}

String literal

"{\"name\":\"Ada\",\"note\":\"line one\nline two\"}"

What options and edge cases does JSON Stringify support?

Escaping rules and options
ParameterTypeDefaultBehaviour & edge cases
Wrap in quotesbooleantrueEmits the surrounding double quotes so the result is a complete JSON string. Turn it off when you are pasting into an existing pair of quotes.
Escape non-ASCIIbooleanfalseConverts every codepoint above U+007E to \uXXXX. Unnecessary for UTF-8 targets, and it inflates the output. Astral characters correctly emit a surrogate pair rather than a truncated single escape.
Escape slashesbooleanfalseWrites / as \/. Legal JSON, required by a few log shippers and by JSON embedded in HTML <script> blocks where </script> would otherwise terminate the element early.
Require valid JSONbooleantrueValidates before escaping, so the literal is guaranteed to parse back into the document you started with. Off, it escapes arbitrary text — useful for log lines and shell snippets.
Control charactersalways escaped—RFC 8259 forbids raw characters below U+0020 in a string. \n \r \t \b \f use their shorthands; everything else becomes \u00XX.
Size growth≈ 5–30%—Depends entirely on how many quotes and backslashes the input contains. A JSON document, which is mostly quotes, grows more than prose does.

Frequently asked questions

What is the difference between this and JSON.stringify in JavaScript?

JSON.stringify takes a JavaScript value and produces JSON text. This takes JSON text and produces a JSON string literal containing it — which is what JSON.stringify(someJsonText) does when you hand it a string. The distinction matters: stringifying an object gives you {"a":1}, while stringifying the text {"a":1} gives you "{\"a\":1}". This tool does the second.

Why did my hand-escaped JSON break?

Almost always an ordering mistake. Replacing quotes before backslashes double-escapes the backslash each replacement just introduced, so \" becomes \\" and the string terminates early. It works until the document contains a quote — which for JSON is immediately. Scanning each character exactly once, as this does, removes the possibility.

How do I reverse it?

Use JSON Unescape, which takes the literal and returns the document. It is deliberately tolerant: it copes with a fragment that lost its surrounding quotes, which is the usual state of anything copied out of a log line or a stack trace.

Should I escape forward slashes?

Usually not. JSON permits \/ but no parser requires it. The two cases where it matters: JSON embedded directly in an HTML <script> block, where a literal </script> inside a string would end the element early, and a handful of log shippers that expect the escaped form.

Is my payload sent anywhere?

No. The escaping happens in this browser tab. The documents people embed in other documents are usually fixtures, API bodies and configuration — exactly the material that should not travel through an unknown server to have its quotes changed.

Why does my escaped string break when I paste it into a shell command?

Because two layers of quoting are in play and only one of them is JSON's. This tool produces a valid JSON string literal; the shell then applies its own rules to whatever you paste, and a double quote that JSON wrote as \" is still a double quote to bash. Wrap the whole value in single quotes, or write it to a file and pass the file path instead.