JSON 3 Polyfill

3.3.3 · deprecated · verified Sun Apr 19

JSON 3 was a polyfill designed to provide `JSON.parse` and `JSON.stringify` functionality for older JavaScript environments that lacked native JSON support, specifically targeting ECMAScript 5 and earlier platforms (e.g., Internet Explorer 6-8). It adheres closely to the ECMAScript 5.1 specification, notably employing a recursive descent parser to avoid reliance on `eval`, a common security concern in early JSON implementations. The library's core differentiator was its robust, specification-compliant parsing and stringification for these legacy environments. However, it explicitly deviates from the spec regarding date serialization: it does not define `Date#toISOString()` or `Date#toJSON()`, instead handling date objects internally during `stringify()` operations, serializing them as simplified ISO 8601 strings. This approach aimed to preserve CommonJS compatibility and avoid polluting native prototypes. The project, last updated to version 3.3.3, is now explicitly deprecated and unmaintained. Developers are strongly advised against using it in new projects and should migrate existing applications to leverage the native `JSON` object available in all modern JavaScript environments. Its release cadence was irregular towards the end, as native JSON support became widespread. It was part of the BestieJS family, focusing on cross-platform support and specification adherence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to include json3 as a global polyfill via a script tag and use its `JSON.stringify` and `JSON.parse` functions, including a reviver, and the `JSON3.noConflict` method.

<!-- Include json3 from a CDN in your HTML -->
<script src="//cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<script>
  // After the script is loaded, JSON.parse and JSON.stringify will be available globally
  // They will polyfill the native JSON object if it's missing or incomplete in older browsers.
  const data = { "Hello": 123, "Nested": [4, 5, { "Date": new Date() }] };
  const jsonString = JSON.stringify(data, null, 2);
  console.log('Stringified JSON:', jsonString);
  // Expected output (date format might vary slightly based on environment):
  // Stringified JSON: {
  //   "Hello": 123,
  //   "Nested": [
  //     4,
  //     5,
  //     {
  //       "Date": "2026-04-19T07:42:00.000Z" // Example ISO 8601 format
  //     }
  //   ]
  // }

  const parsedData = JSON.parse(jsonString, function (key, value) {
    if (typeof value === "number") {
      return value % 2 ? "Odd" : "Even";
    }
    return value;
  });
  console.log('Parsed data with reviver:', parsedData);
  // Expected output:
  // Parsed data with reviver: { Hello: 'Odd', Nested: [ 'Even', 'Odd', { Date: '2026-04-19T07:42:00.000Z' } ] }

  // Using JSON3.noConflict() if you need to restore original window.JSON
  // and use JSON3 functions under a different namespace.
  const json3Restored = JSON3.noConflict();
  console.log('JSON object after noConflict:', typeof JSON); // Should show original JSON or undefined
  console.log('json3Restored.stringify:', json3Restored.stringify(data));
</script>

view raw JSON →