JSON and Number Formats
Tags: development, web, json, javascript
Published: 2023-11-14 23:10:08

---
title: JSON and Number Formats
published: true
date: 2023-10-06 00:00:00 UTC
tags: development, web, json, javascript
canonical_url: https://edm00se.io/json-and-number-formats/
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j96p2wnr3see9ovhyv2w.png
---

I know it's been a while, figured I'd pop up write a quick blog.

![a kraken lifting a sailing ship above water, cracking it in half, and dumping the people into the sea](https://edm00se.io/assets/static/kraken.d042d20.e8e2207fa78b3039802ca3c3fede1a47.gif)

Numbers came up as a topic today, you know, thse primitive entities in JavaScript. Yeah, I was surprised as well.

### The Problem

Whether this is truly a problem is debatable. Working in an almost exclusively node and TS/JS context these days, my code lives and dies based on what my runtime supports. Usually this is defined by [node and its implementation of the v8 engine](https://nodejs.dev/en/learn/the-v8-javascript-engine/). Today this means specifically its implementation of [JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/JSON) and its [`parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/JSON/parse) and [`stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/JSON/stringify) methods. I thought knew about everything there was to know about JSON, short of [JSON 5](https://json5.org/) which sadly looks like [it won't be supported by node anytime soon](https://github.com/nodejs/node/issues/40714), but while unsurprised, I learned something today. The short version is that numeric values in JSON strings need to be valid `float`s or `int`s, this shouldn't be a surprise to anyone experienced with JavaScript. Where the rubber meets the road is that in the process of `JSON.stringify`ing or `JSON.parse`ing, any trailing `.0` on a numeric value will be stripped. Of course it still is of the correlating value, and if decimal places are needed, you _could_ pass the value as a string, but that's silly. What's also silly is the inevitable kickback one could get when matching an existing system's output when migrating to a new one.

### Why Is This a "Problem"?

As far as I can tell, the previously generated output was coming from a C based runtime that was generating the output with complete abandon for the ultimate JSON output itself. It succeeded in creating a numeric value that was parsable as either a `float` or `int`, making it technically correct, but mildly unhelpful and an annoyance for me, here _in the future_. JavaScript, JSON parse, and JSON stringify will each strip the `.0` specificity as its unnecessary for it as a numeric value, either as an `int` or `float`; it's just not needed and any need to _display_ the value differently would likely require using [`.toFixed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/Number/toFixed), because that's a function of display, not numeric value. ![Futurama GIF showing a lawyer character stating "you are technically correct, the best kind of correcet"](https://edm00se.io/assets/static/technically-correct.82a2fbd.4f93395593c5d650b97c1990025a3d3e.gif)

### An Example


{% codesandbox json-and-number-precision-wz8hg2 %}

### Summary

When in doubt, check what you know. We'll see whether I need to do some custom string processing or not; preferably the latter. Still, I've gone down [the custom JSON serialization path before](https://edm00se.io/java/custom-json-serilization/), but that was on another platform with different tooling and reasons. Until next time.

[JSON and Number Formats](https://edm00se.io/json-and-number-formats/) was originally published at [edm00se.io](https://edm00se.io/) on 06-Oct-2023.