JSON stringify

What does “” and 2 does in this code ?

JSON.stringify(data , "",2);

Details about it can be found at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

yup… I visited that link earlier …unfortunately …that reference is very much confusing… this snippet is using 3 arguments

I have some wild guess … it seems its replacing spaces by “” …but what is for 2 ?

That link has the documentation. I just copied the relevant bit below:
space Optional
A String or Number object that’s used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it’s longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.

This syantax is confusing

JSON.stringify(value[, replacer[, space]]) // note [ and ,

Is it same as JSON.stringify(value, replacer, space) ?

I understand 2 here is to add 2 whitespace in output for better readability purpose .

I am not yet clear why there is “” as second argument.

It’s the difference between getting formatting like this…

[{"id":"1","first_name":"Ario","last_name":"Noteyoung","email":"anoteyoung0@nhs.uk","gender":"Male","ip_address":"99.5.160.227","ssn":"509-86-9654","credit_card":"5602256742685208","bitcoin":"179BsXQkUuC6NKYNsQkdmKQKbMBPmJtEHB","street_address":"0227 Kropf Court"},{"id":"2","first_name":"Minni","last_name":"Endon","email":"mendon1@netvibes.com","gender":"Female","ip_address":"213.62.229.103","ssn":"765-11-9543","credit_card":"67613037902735554","bitcoin":"135wbMcR98R6hqqWgEJXHZHcanQKGRPwE1","street_address":"90 Sutteridge Way"},
...

and this…


  {
    "id": "1",
    "first_name": "Ario",
    "last_name": "Noteyoung",
    "email": "anoteyoung0@nhs.uk",
    "gender": "Male",
    "ip_address": "99.5.160.227",
    "ssn": "509-86-9654",
    "credit_card": "5602256742685208",
    "bitcoin": "179BsXQkUuC6NKYNsQkdmKQKbMBPmJtEHB",
    "street_address": "0227 Kropf Court"
  },
  {
    "id": "2",
    "first_name": "Minni",
    "last_name": "Endon",
    "email": "mendon1@netvibes.com",
    "gender": "Female",
    "ip_address": "213.62.229.103",
    "ssn": "765-11-9543",
    "credit_card": "67613037902735554",
    "bitcoin": "135wbMcR98R6hqqWgEJXHZHcanQKGRPwE1",
    "street_address": "90 Sutteridge Way"
  },
...

Which would you rather read?

It means that these parameters are optional. However if you want to provide the 3rd, you’ll also have to provide the 2nd since order matters. And if you’re not interested in the replacer, you’ll typically just pass null here (any invalid type seems to do the trick, but I’d suggest to go with null to be more clear that it’s intentionally left out).

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.