Pass integer value in JSON

amala2
Mega Guru

Hi,

I want to pass the Zip Code in JSON format . For example: "ZipCode":87737.

If the zip code starts with 0, answer should be passed as "ZipCode":08773.But when I use parseInt() or + unary operator "0" is getting truncated.

My code looks like this : "ZipCode": +gr.getValue('zip_code')

Please suggest.

Thanks

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

That's because it's an integer. To prefix it with "0", it needs to be converted to a String.

"ZipCode": +gr.getValue('zip_code').toString();

The requirement is to send it as integer not as a String.

Hi @amala 

unfortunately this is not possible. You only can have a leading zero if you send the value as String.

This not a problem of ServiceNow but the way computers handle different data formats.

Ask the consumer of your JSON data whether they can handle String values and convert them back to integer. But to be honest, when they convert the String values back also the leading zero is lost. 

Kind regards
Maik

Integers in computing language do not use leading zeros in integer. Leading zero implies the following number is in octal. Zip codes shouldn't be represented by an octal.

Check with the person making the requirement to make sure if the requirement implies numeric strings (i.e. a string will only integers).

A prefix 0 is used in C to specify string representations of octal numbers, as required by the ANSI C standard for the "strtol" function (the string to long integer converter) in the "stdlib.h" library. Many other programming languages, such as PythonPerlRubyPHP, and the Unix shell bash also follow this specification for converting strings to numbers.

https://en.wikipedia.org/wiki/Leading_zero