Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Second argument in JSON.stringify in JavaScript

In JavaScript's JSON.stringify() function, I occasionally see the following syntax:

JSON.stringify(obj, null, 4)

However, I can't get what the second argument, null, is supposed to do. As long as I know, the above function takes an object as its first argument, and converts it to a string variable. The third argument, 4 in this case, indents and pretty-prints the resultant string object. But I can't see what the second argument tries to do even after I read the explanation on the official document... So what does the argument do? Or is it just there in order to take in the third argument? (But I think then the function should take both argument name and its parameter, such that for example, JSON.stringify(obj, space=4). I'm not sure whether that sort of syntax is allowed in JavaScript, so forgive me if it's not. But I don't know my expectation is correct in the first place, so would like to throw a question anyway).

Thanks.

like image 901
Blaszard Avatar asked Jul 08 '13 23:07

Blaszard


People also ask

What are the arguments of JSON Stringify?

As a function, it takes two parameters: the key and the value being stringified. The object in which the key was found is provided as the replacer 's this context.

Does JSON Stringify escape double quotes?

JSON. stringify does not act like an "identity" function when called on data that has already been converted to JSON. By design, it will escape quote marks, backslashes, etc.

What is the return type of JSON Stringify?

Return Value: It returns a string for a given value. Example: The below is the example of the JSON stringify() Method.

What does toJSON do in JavaScript?

The toJSON() method returns a date object as a string, formatted as a JSON date.


2 Answers

The second parameter can be a function to perform replacement while stringifying.

A null or undefined second parameter means that you want to use standard stringification, without any customization.

From https://developer.mozilla.org/en-US/docs/Using_native_JSON#The_replacer_pa

Starting in Firefox 3.5.4, JSON.stringify() offers additional customization capabilities through the use of optional parameters. The syntax is:

jsonString = JSON.stringify(value [, replacer [, space]])

value The JavaScript object to convert into a JSON string.

replacer A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.

space 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's larger than that. 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. The replacer parameter

The replacer parameter can be either a function or an array. As a function, it takes two parameters, the key and the value being stringified. The object in which the key was found is provided as the replacer's this parameter. Initially it gets called with an empty key representing the object being stringified, and it then gets called for each property on the object or array being stringified. It should return the value that should be added to the JSON string, as follows:

If you return a Number, the string corresponding to that number is used as the value for the property when added to the JSON string. If you return a String, that string is used as the property's value when adding it to the JSON string. If you return a Boolean, "true" or "false" is used as the property's value, as appropriate, when adding it to the JSON string. If you return any other object, the object is recursively stringified into the JSON string, calling the replacer function on each property, unless the object is a function, in which case nothing is added to the JSON string. If you return undefined, the property is not included in the output JSON string. Note: You cannot use the replacer function to remove values from an array. If you return undefined or a function then null is used instead.

Example

function censor(key, value) {
  if (typeof(value) == "string") {
    return undefined;
  }
  return value;
}

var foo = {foundation: "Mozilla", 
           model: "box", 
           week: 45, 
           transport: "car", 
           month: 7};
var jsonString = JSON.stringify(foo, censor);
The resulting JSON string is {"week":45,"month":7}.

If replacer is an array, the array's values indicate the names of the properties in the object that should be included in the resulting JSON string.

like image 167
Paul Avatar answered Oct 06 '22 01:10

Paul


There is no way to pass third parameter without passing second parameter in JavaScript.
So null is a placeholder for replacer function when you need to pass space.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

like image 28
Andrey Shchekin Avatar answered Oct 06 '22 01:10

Andrey Shchekin