Back to Blog
    JSONJavaScriptJSON StringifyTutorial

    What Is JSON Stringify? Complete Guide with Examples

    J
    Jsonkithub Team
    December 15, 2025
    8 min read

    JSON Stringify is a built‑in JavaScript method that converts a JavaScript value, such as an object or array, into a JSON‑formatted string.

    JSON.stringify(value, replacer, space);

    This JSON string can then be sent over HTTP, stored in localStorage, saved in a database, or logged and transmitted between services.

    JSON Stringify Syntax and Parameters

    Basic syntax

    The general syntax of JSON Stringify looks like this.

    JSON.stringify(value, replacer, space);
    • Value: The JavaScript value (object, array, primitive) to convert to a JSON string.
    • Replacer (optional): A function or array used to customize which values are included or how they are transformed.
    • Space (optional): A number or string that controls indentation and spacing in the output for readability, often used when pretty‑printing JSON.

    Understanding value, replacer, and space

    Value: Most commonly an object or array, but can be any serializable JavaScript value.

    Replacer:

    • If a function, it is called for each key–value pair to modify or filter values.
    • If an array, it defines a whitelist of property names to include in the result.

    Space:

    • A number (0–10) indicates how many spaces to use for indentation.
    • A string (up to 10 characters) is used as the indentation text.

    Must Read: JSON Syntax Explained With Examples

    Examples of JSON Stringify in Action

    Converting objects to JSON strings

    const user = { name: "Amit", age: 30, role: "Developer" }; const jsonString = JSON.stringify(user); console.log(jsonString); // {"name":"Amit","age":30,"role":"Developer"}

    This transforms the in‑memory JavaScript object into a compact JSON string suitable for transport or storage.

    Stringifying arrays and nested data

    const users = [ { name: "Amit", age: 30 }, { name: "Sara", age: 27 } ]; const jsonUsers = JSON.stringify(users, null, 2); console.log(jsonUsers);

    Here, the space argument adds indentation, making the JSON easier to read in logs or files.

    Using replacer to filter or transform

    const user = { name: "Amit", password: "secret123", age: 30 }; const safeJson = JSON.stringify( user, (key, value) => (key === "password" ? undefined : value) ); console.log(safeJson); // {"name":"Amit","age":30}

    The replacer function removes sensitive fields such as passwords before serialization.

    Supported and Ignored Data Types

    JSON Stringify supports only data types that have a valid JSON representation.

    Supported types (convert JSON to Stringify):

    • Objects
    • Arrays
    • Strings
    • Numbers (finite)
    • Booleans
    • null

    Ignored or transformed types:

    • undefined, functions, and symbols are omitted from objects and converted to null inside arrays.
    • Infinity, -Infinity, and NaN are converted to null in JSON.

    Example with ignored values:

    const data = { name: "Amit", temp: undefined, log: () => console.log("hi"), [Symbol("id")]: 1 }; console.log(JSON.stringify(data)); // {"name":"Amit"}

    Properties with unsupported values are skipped because JSON is a pure data format and does not support functions or symbols.

    JSON Stringify vs JavaScript Objects

    JavaScript objects and JSON strings serve different purposes even if they look similar.

    Key differences

    • A JavaScript object is an in‑memory structure that your code reads and writes directly.
    • A JSON string is an immutable text representation designed for transmission and storage.

    You can use our JSON Stringify tool to quickly convert objects to JSON strings online.

    Object vs JSON string overview

    AspectJavaScript objectJSON string
    MutabilityMutableImmutable text
    Can contain functionsYesNo, data‑only
    Supports undefinedYesSkipped or converted to null
    Use caseIn‑memory operationsTransport, storage, logging
    TypeObject/ArrayString

    To work with data, you use regular objects; to send or persist data, you use JSON stringify to turn them into strings.

    When and Why to Use JSON Stringify

    Sending data to APIs

    Most HTTP APIs accept JSON strings as the request body.

    const user = { name: "Amit", age: 30 }; fetch("/api/users", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(user) });

    Client‑side frameworks and fetch‑based code rely on JSON stringify to send structured data correctly.

    Storing data in localStorage or databases

    Browser localStorage can only store strings, not raw objects.

    const settings = { theme: "dark", showTips: true }; localStorage.setItem("settings", JSON.stringify(settings));

    Later, you can read the value and parse it back into an object using JSON Parse.

    Logging and exporting JSON

    Readable JSON can make debugging much easier.

    const state = { step: 1, status: "pending" }; console.log(JSON.stringify(state, null, 2));

    The space argument formats logs or exported files so they are easier to scan in consoles and log aggregators.

    Common Mistakes and How to Avoid Them

    Converting circular structures

    If an object references itself, JSON stringify throws TypeError: Converting circular structure to JSON.

    const obj = {}; obj.self = obj; JSON.stringify(obj); // TypeError

    To avoid this, remove or replace circular references, or use a custom serializer that tracks visited nodes.

    Expecting functions and undefined to appear

    Functions, undefined, and symbols are not represented in standard JSON.

    const obj = { name: "Amit", log() {}, temp: undefined }; console.log(JSON.stringify(obj)); // {"name":"Amit"}

    Use separate mechanisms to handle behavior (functions) and keep JSON reserved for pure data.

    Forgetting to stringify before sending

    Sending a raw object in fetch or XHR can lead to silent failures or incorrect payloads.

    // ❌ Wrong fetch("/api", { method: "POST", body: { name: "Amit" } // not stringified }); // ✅ Correct fetch("/api", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: "Amit" }) });

    Always confirm that API request bodies and storage APIs receive a JSON string, not an object.

    JSON Stringify vs JSON Parse

    JSON stringify and JSON parse are complementary operations.

    • JSON.stringify(value) converts a JavaScript value into a JSON string.
    • JSON.parse(text) converts a JSON string back into a JavaScript value.

    Typical round‑trip example:

    const original = { name: "Amit", age: 30 }; const serialized = JSON.stringify(original); const restored = JSON.parse(serialized);

    Use JSON stringify for outbound data (to APIs, storage, or logs) and JSON parse for inbound JSON strings from those sources.

    Conclusion: Master JSON Stringify for Safer Data

    What Is JSON stringify is more than a syntax question; it is about understanding how to safely convert in‑memory JavaScript objects into JSON strings for APIs, storage, and logging.

    By learning how value, replacer, and space work, understanding supported data types, and avoiding issues like circular references, developers can use JSON stringify confidently in modern JavaScript applications.

    Tools on JSON Kithub help:

    FAQs On JSON Stringify

    Q1. What is JSON stringify used for?

    JSON stringify converts JavaScript values (like objects or arrays) into JSON strings that can be sent to APIs, stored in localStorage, or written to files.

    Q2. Why does JSON stringify remove undefined or functions?

    Values like undefined, functions, and symbols are not valid JSON types, so they are dropped from objects and converted to null inside arrays.

    Q3. How do I pretty‑print JSON stringify output?

    Pass a number or string as the third space argument (for example, JSON.stringify(obj, null, 2)) to add indentation and make the JSON easier to read.

    Q4. What is the difference between JSON Stringify and JSON Parse?

    JSON stringify turns a JavaScript value into a JSON string, while JSON parse takes a JSON string and reconstructs the original JavaScript value.

    Q5. How can I fix "TypeError: Converting circular structure to JSON"?

    Remove or replace circular references, or use a custom serializer or library that can handle cycles before calling JSON stringify.

    Ready to Try Our JSON Tools?

    Format, validate, and transform your JSON data with our free online tools.