cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

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

evanfebrianto
7-Bedrock

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

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.

 
6 REPLIES 6

@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!

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.

@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;

Hi @nmilleson , I am using POSTMAN. This is the service that I updated using your code.

evanfebrianto_0-1601647536932.png

This is the result returned to the POSTMAN when I invoke the service.

evanfebrianto_1-1601647572643.png

 

@evanfebrianto ,

 

Perhaps that's just something that Postman does.  Does it throw an error in your 3rd party software?

Yes, it does. This is the error log from the software.

evanfebrianto_1-1601655837283.png

 

As you can see that the software is expecting an integer. However, somehow it detects the ReturnCode data type as a string even though I stated that it is an integer. Any ideas on this?

 

Top Tags