JSON Parse Reviver Source Text
Read each value's original source text in the reviver so decimal literals like 19.99 survive as exact strings instead of rounded floats.
Code
// The reviver's third argument (ES2025) exposes the source text of each value.
// Decimal literals like 19.99 lose precision as floats, but context.source
// keeps them exactly as written in the document.
const catalog = '{"sku": "ts-100", "price": 19.99, "stock": 42}';
const product = JSON.parse(catalog, (key, value, context) => {
if (key === "price" && typeof value === "number" && !Number.isInteger(value)) {
return context.source; // "19.99" verbatim, no binary-float rounding
}
return value;
});
console.log("price type:", typeof product.price, "value:", product.price);
console.log("stock untouched:", product.stock, typeof product.stock);
// Strings come back with their quotes included in the source
const quoted = [];
JSON.parse('{"name": "Ada"}', (key, value, context) => {
if (key === "name") quoted.push(context.source);
return value;
});
console.log("string source keeps quotes:", quoted);Output
Captured from a sandboxed run of this snippet in the build pipeline.
price type: string value: 19.99
stock untouched: 42 number
string source keeps quotes: [ '"Ada"' ]What this snippet does
Demo state sits in top-level bindings: catalog, product, quoted.
What to watch for
- No third-party code is loaded at any point while this runs.
Applying it
The output labels every step: price type:, stock untouched:, string source keeps quotes:.
Statements that matter
return context.source; // "19.99" verbatim, no binary-float rounding
}
JSON.parse('{"name": "Ada"}', (key, value, context) => {
if (key === "name") quoted.push(context.source);
return value;
});
Interactive Tools for This Topic
CSV to JSON ConverterConvert comma-separated values into a JSON array of objects using the first row as field names, with support for quoted and escaped fields.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON Diff ViewerCompare two JSON documents and list every added, removed, and changed path in a structural diff, with clear before and after values.
FAQ
- Any setup needed beyond the language runtime?
- None — the language runtime is the only requirement. Its state lives in module-level bindings: `catalog`, `product`, `quoted`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.