
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2021 04:00 PM
I am getting some strange behavior when trying to return an integer in a Scripted Rest API response. So given the sample the below, the field u_birth_month is set to an integer in the table sys_user.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var user_list = [];
var gr = new GlideRecord("sys_user");
gr.orderBy('employee_number');
gr.addNotNullQuery('u_birth_month');
gr.query();
while (gr.next()) {
//var int_value = Number(gr.getValue('u_birth_month'));
//var int_value = Math.floor(parseFloat(gr.getValue('u_birth_month')));
//var int_value = parseInt(gr.getValue('u_birth_month'));
//var int_value = parseInt("6");
//var int_value = 6;
var int_value = parseInt(gr.getValue('u_birth_month'));
var user_obj = {
employee_number: gr.getValue("employee_number"),
u_birth_month: int_value
};
user_list.push(user_obj);
}
return user_list;
})(request, response);
When testing the call in PostMan, u_birth_month is always returning a decimal. Nothing that i've tried (see the commented lines within the while loop) seems to work in removing the decimal.
I initially though that perhaps Postman was rendering all integers as decimals, but that is not the case as I can simply hardcode an integer and it renders correctly in Postman.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2021 11:58 PM
The idea is that both JavaScript and Rhino/Java represent numbers as floats. Perhaps that is why when Java represents the integer 6 it adds one decimal. Also I doubt you can do something about it.
I suppose if the receiving end can't handle integers expressed with decimals, you can do it hard core: generate the JSON yourself, and use response.getStreamWriter() to obtain a pointer to the output stream and write your JSON string to it. For inspiration, you could have a look at Scripted REST Resource named GetCITypes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2021 11:54 PM
That is true - I thought you were after the string 6.
But if your goal is to send a(n integer) number, than 6 or 6.0 or 6.00 or 6.000 a.s.o. are the same and are integers no matter how many decimal 0s you add.
In that case, where a(n integer) number is expected, the original solution should be fine as is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2021 11:58 PM
The idea is that both JavaScript and Rhino/Java represent numbers as floats. Perhaps that is why when Java represents the integer 6 it adds one decimal. Also I doubt you can do something about it.
I suppose if the receiving end can't handle integers expressed with decimals, you can do it hard core: generate the JSON yourself, and use response.getStreamWriter() to obtain a pointer to the output stream and write your JSON string to it. For inspiration, you could have a look at Scripted REST Resource named GetCITypes.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 07:23 AM
For giggles I tried this:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var user_list = [];
var gr = new GlideRecord("sys_user");
gr.orderBy('employee_number');
gr.addNotNullQuery('u_birth_month');
gr.query();
while (gr.next()) {
var int_value = Number(gr.getValue('u_birth_month'));
var user_obj = {
employee_number: gr.getValue("employee_number"),
u_birth_month: int_value
};
user_list.push(user_obj);
}
user_list = JSON.parse(JSON.stringify(user_list));
return user_list;
})(request, response);
... and it worked... and yes it's embarassing.
So I ultimately went with your idea to use the reponse.getStreamWriter().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 02:15 PM
I'm glad you managed to sort this out 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2024 05:47 AM
Hey.
Instead of doing JSON.parse and JSON.stringify, you can do it the below way:
var user_obj = {
employee_number: gr.getValue("employee_number"),
u_birth_month: GlideStringUtil.parseLong(int_value)
};