JSON 3 Polyfill
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
-
JSON.parse is not a function
cause Running in an older browser environment where native `JSON` is unavailable, and the `json3` polyfill has either not been loaded or failed to execute correctly.fixEnsure the `json3` script is included in your HTML `<head>` or at the beginning of your JavaScript bundle, before any code that attempts to use `JSON.parse` or `JSON.stringify`. Verify that the script loaded without errors (check browser developer console). -
Uncaught TypeError: Cannot read property 'stringify' of undefined
cause Attempting to use `JSON.stringify` (or `parse`) in a modular context (e.g., CommonJS or ESM) without `json3` correctly polyfilling the global `JSON` object, as `json3` is primarily designed for global script inclusion.fixjson3 is intended to be a global polyfill. Include it via a `<script>` tag in your HTML. If using a module bundler, ensure it processes the script in a way that exposes `JSON` globally (e.g., using `script-loader` for Webpack or similar global injection methods). -
Date objects are not serialized as expected to ISO 8601 strings.
cause json3's custom date serialization logic does not rely on `Date#toJSON()` or `Date#toISOString()`, which can lead to different output formats compared to native `JSON.stringify()` or other polyfills.fixIf a specific ISO 8601 date string format is required, manually transform `Date` objects into the desired string format before passing them to `JSON.stringify`. For example, `JSON.stringify({ date: myDate.toISOString() })`.
Warnings
- breaking The json3 package is officially deprecated and no longer maintained. Using it in new projects is strongly discouraged, and existing projects should migrate to native JSON functionality.
- gotcha JSON 3 does not define `Date#toISOString()` or `Date#toJSON()`, deviating from the ECMAScript specification. Date serialization is handled internally by its `stringify()` implementation, which can lead to inconsistencies if other parts of the application expect these methods to exist or behave differently.
- gotcha In environments where native `Date#toJSON()` implementations do not conform to the spec, JSON 3 will override the native `JSON.stringify()`. This can cause unexpected behavior or performance regressions if the native implementation was superior or if other code relies on the native `Date.toJSON` behavior.
- gotcha As an unmaintained library, json3 will not receive security updates or bug fixes. Relying on it for critical applications in environments where native JSON is absent exposes your application to potential vulnerabilities or undefined behavior discovered post-deprecation.
Install
-
npm install json3 -
yarn add json3 -
pnpm add json3
Imports
- JSON.parse
import { parse } from 'json3'JSON.parse('{"key":"value"}') - JSON.stringify
import { stringify } from 'json3'JSON.stringify({ key: 'value' }) - JSON3.noConflict
JSON.noConflict()
JSON3.noConflict()
Quickstart
<!-- 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>