Skip to main content

JSON Stringify: Serialise JSON for Code

JSON.stringify() converts a JavaScript value into a JSON string. This tool lets you paste any JSON, choose your indent level, and instantly see the stringified output — ready to embed in your source code or send over the wire.

  • Compact output: Zero indent removes all whitespace — ideal for API payloads.
  • Indented output: Choose 2, 4 spaces or tabs for readable code.
  • String literal mode: Wraps the result in quotes with internal quotes escaped — paste straight into code.
  • Privacy-First: All processing is done locally in your browser.

JSON.stringify() Quick Reference

The full signature is JSON.stringify(value, replacer, space). The space argument controls indentation (0 = compact, 2 or 4 = readable). A replacer function or array can filter which keys are included.

Call Output style
JSON.stringify(obj) Compact — one line, no spaces
JSON.stringify(obj, null, 2) Indented with 2 spaces
JSON.stringify(obj, null, 4) Indented with 4 spaces
JSON.stringify(obj, null, "\t") Indented with tabs
JSON.stringify(obj, ["id", "name"]) Only id and name keys included
Reference guide

Stringify in Practice

Input JSON

                    {
  "id": 1,
  "name": "Alice",
  "active": true
}
                

Compact (indent 0)

{"id":1,"name":"Alice","active":true}

As JS string literal

"{\"id\":1,\"name\":\"Alice\",\"active\":true}"

Common Use Cases

Embed in Source Code

Copy a compact JSON string literal to hardcode data in JavaScript, TypeScript, or Python source files.

API Payload Prep

Convert a pretty-printed JSON object to a minimal single-line string ready to paste into curl or Postman. For maximum size reduction, follow up with our JSON Minifier.

localStorage / Cookies

Serialise objects before storing them in localStorage.setItem() or encoding into URL parameters.

JSON.stringify Deep Dive: Replacer, Space & Pretty-Printing

Most developers only use the basic JSON.stringify(value) call, but the full three-argument signature JSON.stringify(value, replacer, space) unlocks powerful serialization control. The replacer argument can be an array of key names to include (a whitelist) or a function that receives each key-value pair and returns the serialize value, enabling you to exclude sensitive fields, transform dates, or handle non-standard types like Map and Set. The space argument controls indentation: pass 2 for two-space pretty-printing or '\t' for tab indentation — making JSON human-readable in config files, debug logs, and documentation.

The reviver function in JSON.parse(text, reviver) mirrors the replacer — transforming each parsed key-value pair back to the original type. This is the standard pattern for deserializing ISO 8601 date strings back to Date objects, or reconstructing BigInt values that have no native JSON representation. Together, a matched replacer/reviver pair gives you full round-trip fidelity for complex object graphs.

Common JSON.stringify Pitfalls: undefined, Circular References & BigInt

JSON.stringify silently drops properties with undefined values, functions, and Symbol keys — a source of many subtle bugs. If you stringify {"name":"Alice","score":undefined} you get {"name":"Alice"} with no warning. Arrays containing undefined elements serialize those positions to null. Always verify your stringified output doesn't contain phantom omissions before sending it across the wire.

Circular reference errors (TypeError: Converting circular structure to JSON) occur when an object directly or indirectly references itself. The standard fix is a custom replacer that tracks seen objects with a WeakSet and returns undefined for circular entries, or using packages like flatted or json-stringify-safe. BigInt values also throw a TypeError by default — you need to add a toJSON() method on the BigInt prototype or use a replacer that converts them to strings.

For a complete reference including performance benchmarks, replacer patterns, and serializing custom classes, see our complete JSON.stringify guide for JavaScript developers.

JSON.stringify for APIs, WebSockets & Node.js Streams

JSON serialization is the backbone of HTTP API communication. Every fetch() call with a POST body, every WebSocket message, and every Redis pub/sub payload relies on JSON.stringify to convert your JavaScript objects to a transmittable string. In Node.js streams, you can pipe JSON-serialized records into a writable stream for efficient file writing or database bulk inserts. For maximum throughput in high-concurrency services, consider fast-json-stringify which generates optimized serializers from JSON Schema definitions — up to 5x faster than the native implementation for hot code paths. Complement your serialization workflow with our JSON Minifier to strip whitespace from production payloads, or use our JWT Encoder to sign serialized JSON claims as a secure token.


Frequently Asked Questions

Is my data safe with this JSON tool?

Yes. This tool uses 100% client-side processing. Your JSON data never leaves your browser and is never sent to our servers, ensuring maximum privacy and security.

Does this tool work offline?

Once the page has loaded, all processing happens locally in your browser. You can disconnect from the internet and the tool will continue to work — no server connection is required to format, validate, or convert your JSON.

Is there a file size limit?

No server-side limits apply because everything runs in your browser. Practical limits depend on your device's memory, but modern browsers handle JSON files of tens of megabytes without issue.

What is the difference between compact and string literal output?

Compact output is the raw JSON string — useful for API calls or storage. String literal mode wraps the result in double quotes and escapes internal quotes (\"), so you can paste it directly as a string value in JavaScript or Python source code.

Does JSON.stringify preserve number precision?

JSON.stringify uses standard JavaScript number serialisation. Very large integers (> 2^53) may lose precision because JavaScript represents all numbers as 64-bit floats. Use a BigInt-aware serialiser for such values.

What values does JSON.stringify skip?

JSON.stringify omits object properties whose value is undefined, a function, or a Symbol. In arrays, such values are serialised as null. Use a replacer function if you need to handle them differently.