Skip to main content
JavaScript & Node.js Serialization

JavaScript JSON Serializer & Parser Playground

Deep dive into JSON.parse and JSON.stringify. Test custom revivers, replacers, safe parsing patterns, and generate production-ready Node.js file operations.

Input JSON String
Evaluated JavaScript Code & Output

JSON.parse vs JSON.stringify in Modern JavaScript

JSON (JavaScript Object Notation) is the backbone of web APIs. While JSON.parse() and JSON.stringify() appear straightforward, mastering their advanced parameters (reviver callbacks, replacer arrays, indent spacing) is essential for avoiding production bugs.

1. Transforming Dates with a Reviver

Standard JSON does not define a native date type; dates are serialized as ISO 8601 strings. By supplying a reviver function to JSON.parse(), string timestamps are automatically reconstituted into real Date objects:

const data = JSON.parse(rawJson, (key, value) => {
  if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T/.test(value)) {
    return new Date(value);
  }
  return value;
});

2. Safe Parsing with Error Boundaries

Never call JSON.parse() directly on unvalidated network input without an error handler. An unexpected character will crash the thread:

function safeJsonParse(str, fallback = null) {
  try {
    return JSON.parse(str);
  } catch (err) {
    console.warn("JSON Parse Failed:", err.message);
    return fallback;
  }
}

Frequently Asked Questions

JSON.parse() deserializes a JSON string into a native JavaScript object, array, or primitive. Conversely, JSON.stringify() serializes an in-memory JavaScript data structure into a formatted JSON string representation.
A reviver function is an optional transformation callback passed as the second argument to JSON.parse(text, reviver). It allows developers to intercept and convert values during parsing—such as converting ISO 8601 date strings into native Date objects.
Directly invoking JSON.parse() on malformed input throws a fatal SyntaxError. Always wrap calls in a try...catch block or use a safe parsing helper function that returns null or a fallback default value upon parse failure.
Per the JSON specification (RFC 8259), JSON only supports string, number, boolean, null, object, and array types. When serializing objects, properties whose values are undefined, functions, or Symbols are omitted.