Skip to main content
1-Visitor
September 29, 2020
Question

How to convert number to integer in JSON output of a service?

  • September 29, 2020
  • 1 reply
  • 8353 views

Hi, I have a service that outputs a JSON. The service is as simple as

var result = {
    "ReturnCode": 0,
    "Message": ""
};

However, when I called the service using POSTMAN, it returns 

{
    "ReturnCode"0.0,
    "Message"""
}

 

I tried parseInt("0") to output integer for ReturnCode yet it doesn't work. How to convert the JSON format from number to integer?

 

Thanks.

 

1 reply

17-Peridot
September 29, 2020

@evanfebrianto 

 

Do you need that for calculation purposes or just display purposes?  If it’s just for display, you could return it as a string instead of a number. If it’s for a calculation, the decimal shouldn’t matter too much; and if it’s for some logical ‘if, while, etc...” test and you’re worried about testing an int vs [float,double,numeric,etc], then perhaps you can use whatever program you’re calling the service in to handle the type-casting of that number. Hope that helps!

1-Visitor
October 2, 2020

Thank you for your response. I think I can't do that because I want to integrate with the other 3rd party software which I can't modify the function to cast the data type. I understand about casting in Thingworx or even if it is just for display. However, I need this to integrate as an integer is a compulsory data type.

17-Peridot
October 2, 2020

@evanfebrianto ,

 

What client are you seeing the decimal in?  I did a quick test in Chrome ARC and I'm only getting integers:

 

{

"parsedCode": 0
"fixedCode": "0"
"rawCode": 0

}

 

ThingWorx service code:

var res = {};

var code = 0;

res.rawCode = code;
res.parsedCode = parseInt(code);
res.fixedCode = code.toFixed();

var result = res;