Category: Data Source
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

API return long number not correct

Issue

I am trying to retrieve a value from an API that is larger than 17 bits, resulting in the last three bits of the JSON data being 0. This is because the original value type is long and its precision is reduced after being converted to JSON. I considered parsing with XMLHttpRequest, but Appsmith has closed XMLHttpRequest and the API provider cannot provide support. Therefore, the only workaround is to return the value as a string due to a JavaScript limitation of integers not being able to exceed 16 digits long. I will need to ask the API provider for help with this issue.

Resolution

If the value returned by the API exceeds 17 bits, resulting in the last three bits of JSON data being 0, the precision is reduced after it is converted to JSON. This is a JavaScript limitation, as integers cannot be higher than 16 digits long. The only workaround is to return them as strings.

To get the accurate value, you will need to ask the API provider to return the value as a string instead of a long. If parsing with XMLHttpRequest, you would need to use the .responseText property instead of .response to avoid the JavaScript limitation.

Example code for parsing with XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'url/to/api', true);
xhr.onload = function () {
var data = JSON.parse(xhr.responseText);
var value = data.value;
// use the string value as needed
};
xhr.send();