r/coding • u/fagnerbrack • Oct 11 '26
Your JSON Is Lying to You
https://blog.gaborkoos.com/posts/2026-08-03-Your-JSON-Is-Lying-to-You/
7
Upvotes
r/coding • u/fagnerbrack • Oct 11 '26
4
u/fagnerbrack Oct 11 '26
For a quick glance:
A JSON.stringify/parse round trip in JavaScript can quietly change data and still produce valid JSON. Integers above 253 lose precision when JS parses them, and BigInt throws, so exact IDs should travel as decimal strings. Undefined properties vanish, which collapses "absent" and "undefined" into one state, and in arrays they turn into null. Key order holds inside JS, but RFC 8259 treats objects as unordered, so meaningful order belongs in arrays, and hashing or signing needs canonicalization. Dates become UTC strings and lose their offset. Map, Set, RegExp and Error become empty objects, NaN and Infinity become null, and class instances lose their prototype. Serialization also runs code through toJSON, getters and replacers, and any of these can throw.
If the summary seems inacurate, just downvote and I'll try to delete the comment eventually 👍
Click here for more info, I read all comments
29
u/Shaper_pmp Oct 11 '26
These are all true, but should not be surprising to anyone who remembers that JSON is a text-based data format designed for storing in text files or transmitting over a network.
JSON is a notation, not a JS object, so it can't contain JS Objects (only portable serialisations of their data), references to other JS Objects in memory, it doesn't differentiate between absent an undefined (just as most idiomatic JS code doesn't), etc, etc.
Treat it as a lightweight, text-based data interchange format and you're golden. Think of it as as just another form of "live" JS Object and you'll trip over every one of these issues because it ain't that.
12
u/exodusTay Oct 11 '26
Title makes it sound like a JSON gotcha you need to look out for but all of these are JS gotchas.
5
u/plastic_eagle Oct 15 '26
Holy slop-tastic, Batman. An article that could have been about five lines is pages and pages of unreadable crap!
I mean, it includes the gem
"
JSON.parsechecks syntax and returns JavaScript values. It does not verify that an object contains the fields your application expects or that those fields satisfy domain constraints."No shit.
1
u/KitKatBarMan Oct 14 '26
Who's Json?
-1
u/obetu5432 Oct 11 '26
thanks to web "developers", we ended up with the worst serialization format as the most popular one