JSON Serialization of numeric data types

It came to my attention that the website generates the same Base64 string for payloads containing numerical values written in different notations. An interesting example is the output for these two payloads:

{
  "value": 0.000001
}
{
  "value": 1.0e-6
}

Both yield: eyJ2YWx1ZSI6MC4wMDAwMDF9 Upon decoding, they both show: {"value":0.000001}

I am curious about where I can find more information regarding this behavior. I suspect that there might be a formal specification like an RFC or similar documentation. Any informal resources related to this topic would also be highly appreciated!

Answer №1

Regardless of how a number is created and what notation is used for the number literal, the internal representation is always IEEE 754. This means that when serialization occurs, the values will be the same, resulting in the same output.

If you're interested in how numbers are serialized to a string, you can refer to the Spec. In short, the serialization depends on the number of significant digits.

console.log(1e-6 === .000001)

console.log(JSON.stringify(1e-6), 1e-6.toString())
console.log(JSON.stringify(1e-7), 1e-7.toString())

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Activate code coverage for Jest tests within jest-html-reporter/Istanbul

Utilizing the jest-html-reporter package has proven useful in generating an HTML report for my tests. However, while this report displays information on test results (passing and failing), it lacks details regarding code coverage statistics such as lines c ...

Is there a way to efficiently fetch localStorage data upon launching a React application and seamlessly store it in Redux using the useEffect hook without experiencing any data loss issues?

Should I avoid mixing React hooks and mapStateToProps in my application and instead use Redux hooks like useSelector? In my React app, I have an array of 'friend' data stored in Redux. Using useEffect in one of my components, I am saving this da ...

Having trouble with publishing to npm as public with the --access flag not functioning properly?

When trying to publish a fresh scoped package on NPM using the command npm publish --access public, I encountered the following error: ole@mki:~/cli$ npm publish --access public npm ERR! publish Failed PUT 403 npm ERR! code E403 npm ERR! F ...

What is the purpose of creating a new HTTP instance for Socket.io when we already have an existing Express server in place?

As I delve into SocketIO, I've combed through various blogs and documentation on sockets. It seems that in most cases, the standard approach involves creating an HTTP server first and then attaching the socket to it as shown below: var app = express() ...

A step-by-step guide on adjusting the bar chart width using a slider tool in Vega

In my Vega chart, I am trying to dynamically change the width of a bar based on the position of a slider. Here is the code snippet: { "$schema": "https://vega.github.io/schema/vega/v4.json", "width": 400, "height": 50, "padding ...

Is there a way for me to incorporate the input parameter value passed to a JavaScript function into the popup that is being opened by the function itself?

I am not very proficient in PHP and JavaScript, and I'm facing an issue while trying to pass a value from a main page to a popup page that is defined within this main page. To provide more context: In my account.php page, there is a link like this: ...

Sending the value from a for loop through AJAX and populating it into a form field

Currently, I have implemented a piece of JavaScript code that captures user input, sends a request to an endpoint using AJAX, and appends a specific field from the returned results as an option within a datalist. This functionality is working perfectly fin ...

Integrate cart feature similar to the shopping experience on Zopnow app

I'm working on integrating an add to cart feature similar to the one in the Zopnow app. Does anyone understand the concept behind this functionality? It's really impressive. ...

JavaScript time to stop typing issues

Here's a functional JSfiddle I created for this question: http://jsfiddle.net/KSCLC/ I'm working on a simple page where you can either scan or manually type in a UPC, which then automatically gets added to a table for display. I came up with som ...

Extract key/value pairs from a JSON array by eliminating any HTML tags

I have a JSON array payload that I need to break down into a separate object for downstream processing. The payload is flexible and may contain multiple nested levels in the JSON array, but the first level will always include an id field as the unique ide ...

What is the best way to handle the resolution of multiple promises as they complete?

Suppose I have three different promises each taking a varying amount of time to resolve - 1000ms, 2000ms, and 3000ms respectively. How can I simultaneously start all the promises and handle them as they get resolved? For instance: let quickPromise = new ...

How can I transform an HTML div into a video file like MP4 using Python and Django?

I'm looking to take a HTML page and target a specific <div> within it in order to convert it into video format. Purpose: I understand that HTML is typically static, but I have a requirement to transform it into a video. I'm seeking method ...

Obtaining comprehensive error information during the deserialization of JSON data can be achieved by implementing

Currently, I have been utilizing the JSon.net library for deserializing certain objects and it has been functioning smoothly: Public Class Person Public Property PersonId As Long Public Property Name As String Public Property SSN As Integer End Clas ...

The rendering of graphs in FusionCharts is experiencing delays particularly in Internet Explorer, with Chrome performing more efficiently in comparison

I am currently utilizing FusionCharts to generate and display graphs. My requirement is to load over 60 graphs on a single page. Upon testing the page loading in Internet Explorer 11, it is taking approximately 5 minutes. However, when using Google Chrom ...

Executing the event handler only once

In my React project, I have a button that toggles a boolean state. However, I realized that the button can both set and unset the state due to its toggle functionality. I only want the state to be changed once. Is there a different function I can use ins ...

An issue occurred with Meteor while attempting to load or refresh the page: A Tracker afterFlush function threw an exception that is undefined

Encountering an error resulting in a blank page, with the exception of the navbar, when refreshing or directly accessing the link in a browser. This issue arises consistently under these circumstances, whereas navigation through the templates works fine. I ...

The error message 'ReferenceError client is not defined' is indicating that the

I'm currently attempting to retrieve the id of clients connecting to my socket.io/node.js server by following the method outlined in the top response on how to get session id of socket.io client in Client. However, I am encountering an error message: ...

Finding all items in an array in Cypress and validating them using JavaScript assertions

After making an API call, I have received an array response that includes the following information: [ { "IsDatalakeEnabled": true, "RecoveryHr": { "IsRecoveryHREnabled": false, &quo ...

JavaScript that is subtle and lacks function parameters

Suppose you have: <div id="data" onclick="handleData(1)">Datum 1</div> and you prefer late binding instead: <div id="data">Datum 1</div> <script> $("#data").click(function() { handleData(1); }) </script&g ...

Connect an EventListener in JavaScript to update the currentTime of an HTML5 media element

*update I have made some changes to my code and it is now working. Here's the link: I am trying to pass a JavaScript variable to an HTML link... You can find my original question here: HTML5 video get currentTime not working with media events javscr ...