"category": "utility",

JSON Stringify - Online Tool and JSON.stringify() Guide

"tldr": Paste JSON and get it as an escaped string literal - minified, quoted, and ready to embed inside another JSON document or code.

Turn a JSON document into an escaped string literal: the input is validated, minified, and wrapped in quotes with every inner quote escaped - the exact form you need when a JSON document must live inside a JSON string field, a code literal, or an environment variable.

{"json": "string literal"}

This "JSON inside JSON" shape appears everywhere: webhook payload fields, database TEXT columns, message-queue envelopes, and Terraform/CloudFormation templates. Doing the escaping by hand is the fastest way to produce the parse errors our error guides exist for.

In JavaScript the same operation is JSON.stringify(value, replacer, space): it turns an object, array, or primitive into a JSON string, recursing through nested values and calling toJSON() where one exists (which is why a Date becomes an ISO date string). The questions below cover what it returns, what it silently drops, and how its two optional parameters work.

How to stringify JSON

  1. 1Paste your JSON - it is validated first, so you never stringify a broken document.
  2. 2The document is minified, then escaped into a double-quoted string literal.
  3. 3Paste the result as a value inside another JSON document or a code string.
  4. 4To reverse it, use our JSON Unescape tool.

Convert JSON to String literal in code

JSON.stringify() basics
const user = { id: 7, name: "Ada", born: new Date(Date.UTC(1815, 11, 10)), greet() {}, nick: undefined };

JSON.stringify(user);
// '{"id":7,"name":"Ada","born":"1815-12-10T00:00:00.000Z"}'  (greet and nick dropped)

JSON.stringify([1, undefined, NaN]);
// '[1,null,null]'

JSON.stringify(user, ["id", "name"], 2);
// pretty-printed, only id and name
JavaScript
const payload = { event: "order.created", data: { id: 1042 } };
const embedded = JSON.stringify(JSON.stringify(payload));
// "{"event":"order.created",...}" - paste as a JSON string value
Python
import json

payload = {"event": "order.created", "data": {"id": 1042}}
embedded = json.dumps(json.dumps(payload))

Frequently asked questions

What does JSON.stringify() do?

It converts a JavaScript value into a JSON-formatted string: JSON.stringify({ name: "Ada", tags: ["x"] }) returns '{"name":"Ada","tags":["x"]}'. Nested objects and arrays are serialized recursively, and any value with a toJSON() method is replaced by that method's result first. The signature is JSON.stringify(value, replacer?, space?).

What does JSON.stringify() return?

A string in almost every case, but undefined when the top-level value is undefined, a function, or a symbol. Inside objects, properties whose values are undefined, functions, or symbols are omitted; inside arrays the same values become null. NaN and Infinity become null, Dates become ISO strings, and Map and Set become {}. A BigInt or a circular reference throws a TypeError ("Converting circular structure to JSON").

How do the replacer and space parameters work?

replacer is either an array of property names to keep, e.g. JSON.stringify(user, ["id", "name"]), or a function (key, value) => newValue that can transform values or drop them by returning undefined. space controls indentation: a number of spaces (capped at 10) or a string such as "\t". JSON.stringify(obj, null, 2) is the usual pretty-print call.

When should I use JSON.stringify()?

Whenever a value has to become text: the body of a fetch() request with Content-Type: application/json, values written to localStorage or sessionStorage (which only store strings), log lines, and files. Avoid it for deep cloning (structuredClone() keeps Dates, Maps, and Sets) and for comparing objects, because the output depends on key order.

How do I convert JSON to a string in other languages?

Python: json.dumps(obj). Java: new ObjectMapper().writeValueAsString(obj) with Jackson, or new Gson().toJson(obj). C#: JsonSerializer.Serialize(obj). Go: json.Marshal(v), which returns bytes. PHP: json_encode($value). The reverse of each is the matching parse call, e.g. JSON.parse() in JavaScript.

How is this different from JSON minify or JSON escape?

Minify outputs the same JSON, just compact. Escape treats input as arbitrary text. Stringify does both steps for JSON specifically: validate → minify → escape into one string literal, i.e. JSON.stringify applied to a JSON string. Three tools because the three intents produce different output.

When would JSON legitimately live inside a JSON string?

Envelope patterns: a queue message with a "payload" string field, audit logs storing the raw request body, webhook signatures computed over the exact string, and APIs that forward opaque documents without parsing them. It costs double parsing on the consumer side - use nested objects instead when you control both ends.

Why is the output minified first?

Escaped newlines (\n) and indentation spaces survive stringification and bloat the literal by 30-50% while adding nothing - the inner document parses identically. Minifying first keeps the embedded string as small as possible.

How do I do this in code?

JavaScript: JSON.stringify(JSON.stringify(obj)) - the inner call serializes, the outer escapes. Python: json.dumps(json.dumps(obj)). If you find yourself doing it more than twice in one payload, reconsider the envelope design.

Last updated:

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →