rawJSON Preserves Exact Number Text
JSON.rawJSON embeds the exact digit text into JSON output, keeping 9007199254740993 and 1.00 intact where regular numbers round and normalize.
Code
// JSON.rawJSON stores the exact digit text instead of a rounded float.
const ledger = {
id: JSON.rawJSON('9007199254740993'),
balance: JSON.rawJSON('0.1'),
};
console.log('rawJSON :', JSON.stringify(ledger));
// A regular number is silently rounded past Number.MAX_SAFE_INTEGER
const plain = JSON.stringify({ id: 9007199254740993, balance: 0.1 });
console.log('plain value:', plain);
// The reviver's third argument carries the original source text
const back = JSON.parse(JSON.stringify(ledger), (key, value, context) =>
key === 'id' && typeof context?.source === 'string' ? context.source : value,
);
console.log('round trip :', back.id, typeof back.id);
// Trailing zeros survive too: 1.0 and 1.00 stay distinct texts
console.log('zeros kept :', JSON.stringify([JSON.rawJSON('1.0'), JSON.rawJSON('1.00')]));Output
Captured from a sandboxed run of this snippet in the build pipeline.
rawJSON : {"id":9007199254740993,"balance":0.1}
plain value: {"id":9007199254740992,"balance":0.1}
round trip : 9007199254740993 string
zeros kept : [1.0,1.00]How it works
Top-level bindings ledger, plain, back hold the demo state — nothing sits inside a function scope.
What to watch for
- Nothing to vet in a supply-chain review — the file pulls in no packages.
Applying it
The demo output is labeled (rawJSON :, plain value:, round trip :), so every line of output can be traced to the operation that made it.
Statements that matter
const back = JSON.parse(JSON.stringify(ledger), (key, value, context) =>
key === 'id' && typeof context?.source === 'string' ? context.source : value,
);
const plain = JSON.stringify({ id: 9007199254740993, balance: 0.1 });
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
- Will the snippet run without installing packages?
- Yes, it runs as-is — nothing outside the built-in APIs is referenced. Its state lives in module-level bindings: `ledger`, `plain`, `back`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.