Search code examples
jsonzerostringify

Can I use JSON.Stringify without it removing the leading zeros


I have a form that needs to collect the values and turn into a JSON query. However, when I use JSON.stringify it removes the zero off any zip code that begins with zero. Is there any way to prevent that?

Here is a simple version of the code I'm trying to use:

var zipcode = 02122;
var data = {
   ZipCode : zipcode
}

var jsonQuery = JSON.stringify(data);

Solution

  • You're making a fundamental mistake:

    Zip codes are not numbers



    Numbers are for numeric data – things that you might add or multiply.
    Leading zeroes in a number are intrinsically meaningless, and are not stored anywhere.

    Zip codes are strings that happen to only contain digits.