Why JSON Remains The Web Standard
Cover photo by Ilya Pavlov on Unsplash
JSON is the only data format that actually matters for building web software today. I find that trying to replace it with anything more complex usually leads to headaches for no real benefit.
Why keep it simple
It is text. It is predictable. You can parse it anywhere without needing a custom library for every single platform. Here is how a standard object looks in code:
{ "id": 101, "name": "Dev Tools", "active": true}I admit that JSON falls short if you need complex types like dates or binary data. You are left stringifying things and hoping the client knows how to handle the format. That is a annoying hurdle. Sometimes, you just have to deal with the fact that it is a string-based format and stop wishing for native typed support.
The common complaints
“But JSON is too verbose compared to binary formats like Protocol Buffers!” That is a fair point if you are pushing gigabytes of data over a saturated pipe every millisecond. For 99 percent of applications, the developer time you lose setting up a binary schema far outweighs the bandwidth you save. Most of the time, Gzip handles the bulkiness just fine anyway.
How to work with it
Keep your structures flat. Avoid nesting objects ten levels deep. Deep nesting forces consumers to write defensive code just to check if a property exists. If you own the API, treat your users with kindness.
// Do thisconst user = { name: "Jane", role: "admin" };
// Avoid this messconst data = { u: { n: "Jane", r: "admin" } };If you are using TypeScript, define your types near your fetch calls. It makes the whole experience predictable. Are you actually using a formal schema validation library, or are you still just winging it with types and hoping the backend stays consistent?