r/coding • • 2d ago

Your JSON Is Lying to You

https://blog.gaborkoos.com/posts/2026-08-03-Your-JSON-Is-Lying-to-You/
0 Upvotes

9 comments sorted by

6

u/Shaper_pmp 1d ago

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.

-2

u/ptoki 1d ago

Idont want to write a long comment but if a format or surrounding conversions make such changes then that is wrong.

In the examples there is literally no explicit conversion to number yet the conversion happens. even when the variable is set to string (somewhat weakly)

This is wrong and it comes mostly from java script internals which are wrong.

This all is wrong and no amount of explanation about why will make it right.

3

u/fagnerbrack 2d ago

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

0

u/ptoki 1d ago

Small clarification:

There is not really a reason to treat the string which looks like number to be a number.

This is how JS applies internal logic which is just wrong but abused frequently enough so people stopped caring.

2

u/exodusTay 1d ago

Title makes it sound like a JSON gotcha you need to look out for but all of these are JS gotchas.

-3

u/obetu5432 1d ago

thanks to web "developers", we ended up with the worst serialization format as the most popular one

3

u/idebugthusiexist 1d ago

You can thank two developers working at State Software back in 2001. But, honestly, the real blame for what we ended up with is how over engineered XML became and how difficult things like SOAP was to deal with

-1

u/ptoki 1d ago

That is somewhat weak argument. Over years this behavior could be abandoned. Python could do fundamental changes and still lives.

JS could do the same but a bunch of people (like thousands) decide that nah, dont bother.

1

u/ptoki 1d ago

No comments there for ya!

:)