Search code examples
javascriptjqueryjsonresponseconsole.log

FortiSandbox returns a value and jQuery changes the value when console.log()


I am getting a JSON response from FortiSandbox API, where I can see in the network tab that the value returned from the API is 6994689367114128661, but when I use it to send it or to console.log() the value changes to 6994689367114128000.

After getting the response from the server I immediately console.logged the value and it changed.

My jQuery code

  fetch(apiUrl, requestOptionsJobID)
        .then(responsegetJobStatus => {
            if (!responsegetJobStatus.ok) {
                throw new Error('Network response was not ok');
            }
            return responsegetJobStatus.json();
        })
        .then(getJobIDResult => {
            var ok = getJobIDResult.result.status.message;
            if (ok == 'OK') {
                var getJobJid = getJobIDResult.result.data.jids[0];//here is where the value changes to 6994689367114128000
                console.log(getJobJid);//shows the changed value
            }
            return getJobIDResult;
        }).catch(error => {
        console.error('Eroare get job ID: ', error);
    });

The API response with the wanted value of jid from the API

{
    "id": 17,
    "ver": "2.0",
    "result": {
        "url": "/scan/result/get-jobs-of-submission",
        "status": {
            "code": 0,
            "message": "OK"
        },
        "data": {
            "jids": [
                6994689367114128661
            ],
            "total_jids": 1
        }
    }
}

Solution

  • What is happening is that the number you are receiving is too big to be completely handled by javascript. When a number bigger is received, it looses precision. Without entering into exact details, here's a dumbed down example:

    Let's say Javascript can only "remember" 10 digits. It could easily handle numbers like 235 or 1,235,038 but not quite 123,123,456,456 as it has 12 digits. To remain somewhat accurate, javascript would then take only the 10 first digits and memorize how many it discarded: 1,231,234,564 since it got rid of 2 digits (divided the number by 100, 10^2 or e2), it is able to show the number still, but only the 10 biggest digits will be accurate so it would turn out like this: 1,231,234,564e2 = 123,123,456,400

    This is exactly what you are experiencing. The Number.MAX_SAFE_INTEGER value gives you the biggest number Javascript can handle with precision. Above that, it will start loosing precision. In the console.log below, you can see the biggest SAFE INTEGER, then a comparison if your number is bigger than it, and again the number as Javascript is seeing it.

    console.log(Number.MAX_SAFE_INTEGER, 6994689367114128661 > Number.MAX_SAFE_INTEGER, 6994689367114128661);

    So now, how do we fix this issue? Your best bet is to convert the JSON number into a string. You would use .text() method instead of JSON and manipulate the text to quote the numbers in it. Then parsing the JSON would handle the ID as a string and not a number, since strings are handled completely differently than numbers, you would keep the same value as you received.

    See Parsing big numbers in JSON to strings