Two Meanings of "JSON to String"
"Convert JSON to string" is a query with two different intents depending on context. Meaning 1: serialize a JavaScript object or Python dict into a JSON string — this is what JSON.stringify and json.dumps do. The output is a JSON string that another parser can turn back into an object. This is the vastly more common meaning in web development.
Meaning 2: take an existing JSON string and escape it for embedding inside another string context — usually inside JavaScript source code, a shell command, or a URL. This requires an additional layer of escaping beyond what JSON.stringify produces. This tool supports both — the mode switcher lets you pick minified JSON, pretty JSON, or fully-escaped source-safe string.
Meaning 1 — Serialize Object to JSON String
JavaScript / Node.js
const obj = { name: 'Alice', tags: ['admin', 'user'], active: true };
// Minified — one line, no whitespace
const mini = JSON.stringify(obj);
// {"name":"Alice","tags":["admin","user"],"active":true}
// Pretty — 2-space indented
const pretty = JSON.stringify(obj, null, 2);
// With a replacer function to filter or transform values
const filtered = JSON.stringify(obj, (key, val) => {
if (key === 'password') return undefined; // omit sensitive fields
return val;
});Python
import json
data = {'name': 'Alice', 'tags': ['admin', 'user'], 'active': True}
# Minified
mini = json.dumps(data)
# {"name": "Alice", "tags": ["admin", "user"], "active": true}
# Note: json.dumps has space after colon by default — set separators to remove
really_mini = json.dumps(data, separators=(',', ':'))
# Pretty
pretty = json.dumps(data, indent=2)
# Ensure ASCII disabled — keep unicode literal
unicode_safe = json.dumps({'name': 'Ålice'}, ensure_ascii=False)Meaning 2 — Escape JSON for Source Code
If you want to paste a JSON payload as a string literal inside JavaScript, Python, or another language's source code, the JSON itself must be escaped so its quotes and backslashes don't break the outer language's string syntax. This is called double-escaping and it's common when embedding test fixtures or sample payloads directly in code.
The trick is to escape the JSON output as if it were being embedded in a string. For JavaScript: escape backslashes to \\ and double quotes to \". For a template literal, escape backticks. For a shell heredoc, no additional escaping needed if you use single-quoted delimiters.
// Given a JSON string like: {"name": "Alice"}
// Paste-safe version for JavaScript double-quoted string:
const sample = "{\"name\": \"Alice\"}";
// Paste-safe for template literal:
const sample2 = `{"name": "Alice"}`; // no escaping needed inside `
// Paste-safe for Python single-quoted string:
// sample = '{"name": "Alice"}'
// Paste-safe for Python triple-quoted string (no escaping):
// sample = '''{"name": "Alice"}'''Common Serialization Options
Indent — controls whitespace. null or omitted = minified; 2 = 2-space indent; 4 = 4-space indent; "\t" = tab-indented. Minified is 3-5x smaller and preferred for network transfer; pretty is preferred for debugging and version control.
Replacer / default — function or list that transforms values before serialization. Use for filtering (omitting sensitive fields), transforming (formatting dates), or handling non-JSON types (converting Date to ISO string, Buffer to base64).
ensure_ascii (Python only) — when true (default), non-ASCII characters are escaped as \uXXXX. When false, they appear literally. Set to false when human readability matters and your target system handles UTF-8 (most modern APIs).
Non-Serializable Values
Not every value can be serialized to JSON. JavaScript: undefined, functions, and symbols are silently omitted from objects (converted to null in arrays). Date objects call their toJSON() method which returns ISO string. BigInt throws — you must convert to string manually.
Python: raises TypeError on datetime, Decimal, custom classes, sets, and complex numbers. Handle by passing a default= function: json.dumps(data, default=str) converts non-serializable values via str(). For custom control, write a function that dispatches per type.
Related JSON Tools
- JSON Formatter (Parent Tool) — the underlying formatter used by every variant
- Format JSON Online — general-purpose beautifier with 2/4-space and tab options
- Minify JSON Online — strip whitespace for production payloads
- JSON Validator — validate without formatting
- JSON Diff Checker — compare two JSON objects side-by-side