Everything, Everywhere
Verified Specification | Standardized Formulas | Instant Precision
Secure & Private (Zero Data Retention) Free Access • No Sign-Up

Working with JSON

JavaScript Object Notation (JSON) is the universal format for exchanging structured data across the web.

Try It Yourself

      
📋 Copy Safe JSON Parser & Date Reviver Snippet
// Safe JSON Parsing with Custom Reviver
function safeJsonParse(text, fallback = {}) {
  try {
    return JSON.parse(text, (key, value) => {
      // Automatically deserialize ISO date strings
      if (typeof value === "string" && /^\d{4}-\d{2}-\d{2}T/.test(value)) {
        return new Date(value);
      }
      return value;
    });
  } catch {
    return fallback;
  }
}

⚠️ 5 Fatal Traps & JavaScript Pitfalls

Trap #1: JSON.parse() Throws Fatal SyntaxError on Bad Input
Passing invalid JSON, an empty string, or an HTML error page to JSON.parse() immediately crashes the executing script with a SyntaxError. Always wrap in a try...catch.
Trap #2: Silent Stripping of undefined, Functions, and Symbols
JSON.stringify() silently omits object properties with values of undefined, functions, or Symbols, causing silent data loss.
Trap #3: Circular Reference TypeError
Attempting to serialize an object that references itself throws TypeError: Converting circular structure to JSON.
Trap #4: NaN and Infinity Serialized as null
JSON does not support NaN, Infinity, or -Infinity; serializing them converts them silently to null.
Trap #5: Integer Key Coercion to Strings
In JSON, all dictionary keys must be strings. Parsing JSON converts numeric keys to string properties.

💬 Frequently Asked Questions

What is the replacer parameter in JSON.stringify()?
The replacer can be a filtering array of allowed keys or a mapping function (key, value) => filteredValue to transform data before serialization.
What is the reviver parameter in JSON.parse()?
A reviver function (key, value) => transformedValue inspects and transforms every key-value pair during parsing, commonly used to restore Date objects.
How do you pretty-print JSON with indentation?
Pass the indentation space count as the third argument: JSON.stringify(data, null, 2).
What does the toJSON() method on objects do?
If an object defines a toJSON() method, JSON.stringify() invokes it to obtain the serialized value instead of inspecting the object properties directly.
Why does JSON require double quotes for keys and strings?
The JSON RFC standard strictly mandates double quotes. Single quotes or unquoted keys are invalid JSON.
Sponsored Utility
While You're Here
Sponsored Recommendations
Advertisement