DevNex

JSON Stringify

Escape JSON into a string literal.

Private · Runs entirely in your browser

What is a JSON stringify tool?

It takes a JSON document and gives you back a single JSON string whose contents are that document. Every double quote inside becomes \", every backslash becomes \\, and every line break becomes \n. The result is one quoted value, safe to paste anywhere a string is expected.

The name comes from JSON.stringify, which does exactly this when you hand it a piece of text. Doing it by hand is possible and almost always goes wrong: it is one pass of mechanical substitution, and the character you have to escape is the same character you are using to do the escaping.

Note what this is not. Formatting and minifying take JSON and give back JSON. Stringifying takes JSON and gives back a string — which is itself valid JSON, since a bare string is a legal JSON document, but it is one value rather than the structure you started with.

JSON Stringify features

  • Escape a whole JSON document into one string literal
  • Quotes, backslashes, tabs and line breaks all handled
  • Your indentation is preserved, not reformatted
  • Invalid JSON is refused, with the line and column
  • Copy the result, or download it as a .json file
  • Everything runs locally in your browser

When you actually need this

Two situations account for most of it. The first is a test fixture: a mock response has to live inside a source file as a string, and the payload came out of a network panel with quotes all through it.

const body = "{\"id\":42,\"name\":\"Ada\"}"

The second is configuration. Environment variables, CI settings and Kubernetes secrets hold strings, not structures, so a JSON value going into one has to be escaped first — and a single missed backslash there fails at deploy time rather than at your desk.

The mistake this prevents

Double escaping. If your document has already been escaped once and you escape it again, the \" becomes \\\" and the string no longer parses back to what you meant. It is hard to see by eye, because both versions look approximately like noise.

The check is quick: parse the result once and you should get your document back as text. If you get a string full of backslashes instead, it was escaped twice. This tool parses your input before escaping it, so it can tell you when what you pasted was not JSON in the first place — which is the usual sign that you are on the second pass.

Compact or readable

Your formatting survives, as \n and spaces inside the string. That is usually what you want for a fixture you will read later. When you want the shortest possible literal — a config value, a query parameter — run it through the JSON Minifier first and stringify the result.

If the document does not parse, start with the JSON Formatter, which explains what is wrong rather than just refusing, or the JSON Validator if you also want to know about duplicate keys.

Privacy

The escaping runs entirely in your browser. There is no upload step and no server endpoint behind it, which matters here more than for most of these tools: the things people escape into configuration are often the things they should not paste into a website. Open your developer tools, watch the network panel, and paste something. Nothing is sent.

Questions

What does stringify actually do?

It turns your whole document into a single JSON string. Every quote becomes \" , every backslash becomes \\ , and every line break becomes \n, so the result is one quoted value you can paste where a string is expected. It is the same operation JSON.stringify performs on a piece of text, which is where the name comes from.

Why does the output start and end with a quote?

Because the output is a string, and in JSON a string is written inside double quotes. Those two quotes are part of the value. If you are pasting into source code that already supplies the quotes, leave them off - the escaping inside is the part you needed.

Is this the same as minifying?

No. Minifying removes whitespace and gives you back JSON. Stringifying keeps your document exactly as it is and wraps it in one layer of quoting, so the result is a string that contains JSON rather than JSON itself. The two are often used together: minify first if you want the escaped string to be compact.

Does it keep my formatting?

Yes. Whatever indentation and line breaks you can see are preserved, as \n and spaces inside the escaped string. Only whitespace around the whole document is trimmed, because a trailing newline is invisible in an editor and turns into a surprising \n at the end of the result.

Why does it refuse my input?

Escaping does not need valid JSON - it would happily quote anything - but a tool that hands back an escaped broken document is not helping. So the input is parsed first, and if it does not parse you get the same explanation the formatter would give, with the line and column. If it comes back escaped, it was valid.

How do I get the original JSON back?

Parse it once. In a browser console or Node, JSON.parse on the escaped string returns your document as text. There is no unescape page here yet.