JSON.stringify() Function in JavaScript


Where to get the detailed description of the JSON.stringify() Function in JavaScript?

✍: FYIcenter.com


Here is the detailed description of the JSON.stringify() Function in JavaScript.

Description - The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.

  • Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.
  • If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array). JSON.stringify can also just return undefined when passing in "pure" values like JSON.stringify(function(){}) or JSON.stringify(undefined).
  • All symbol-keyed properties will be completely ignored, even when using the replacer function.
  • Non-enumerable properties will be ignored

Syntax -

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

Parameters -

  • value - Required. The value to convert to a JSON string.
  • replacer - Optional. 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/filtering 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 - 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.

Return value - A JSON string representing the given value.

Exceptions - Throws a SyntaxError exception if the string to parse is not valid JSON.

Examples -

JSON.stringify({});                  // '{}'
JSON.stringify(true);                // 'true'
JSON.stringify('foo');               // '"foo"'
JSON.stringify([1, 'false', false]); // '[1,"false",false]'
JSON.stringify({ x: 5 });            // '{"x":5}'
JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)) 
                                     // '"2006-01-02T15:04:05.000Z"'

For more information on the JSON.stringify() function, see JSON.stringify() reference page.


Using JSON in JavaScript

⇒⇒JSON Tutorials

2017-09-08, 143👍, 0💬