r/coding • • 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

18 comments sorted by

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

0

u/ptoki Oct 12 '26

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.

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.

-3

u/ptoki Oct 12 '26

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.

6

u/Shaper_pmp Oct 13 '26

Exactly. Most (all?) of these issues are nothing to do with JSON.

Whether you agree with them or not, they're Javascript quirks, not JSON quirks.

3

u/ptoki Oct 13 '26

I have one beef with json: no comments.

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.parse checks 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.

4

u/tmax8908 Oct 16 '26

And it doesn’t make me a sandwich. It’s bullshit.

2

u/SquarePixel Oct 16 '26

Yeah, the article is quite silly with its misguided premises.

1

u/KitKatBarMan Oct 14 '26

Who's Json?

1

u/fagnerbrack Oct 14 '26

Ask chatGPT, "he" knows 😆

-1

u/obetu5432 Oct 11 '26

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

7

u/idebugthusiexist Oct 11 '26

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

-3

u/ptoki Oct 12 '26

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 Oct 12 '26

No comments there for ya!

:)

1

u/snerp Oct 13 '26

JSON is the best general purpose serialization format. Other text formats are too verbose or have issues with escaping and binary formats are not easily editable and diff-able like json.