Pass integer value in JSON
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 07:43 PM
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
- Labels:
-
Integrations

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 07:54 PM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 07:56 PM
The requirement is to send it as integer not as a String.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 08:37 PM
Hi
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 08:50 PM
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 Python, Perl, Ruby, PHP, and the Unix shell bash also follow this specification for converting strings to numbers.