JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JSON.stringify()

JSON is used to exchange data to/from a web server. When sending data to a web server, the data has to be a string.

The JSON.stringify() method converts a JavaScript object or value to a JSON string.

This method optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Syntax:
JSON.stringify(value, replacer)

The first parameter specifies the value to convert to a JSON string.

The optional second parameter specifies a function that alters the behavior of the stringification process.


Stringify a JavaScript Object

Suppose we have the following object in JavaScript:

Using the JSON.stringify() method we can convert JavaScript object into JSON string:

Run code

Stringify a JavaScript Array

Suppose we have the following array in JavaScript:

Using the JSON.stringify() method we can convert JavaScript array into JSON string:

Run code

Stringify Dates

Date objects are not allowed in JSON.

The JSON.stringify() method will convert any date object into string.

Run code

Note: Converting a string to a native object is called parsing, while converting a native object to a string so it can be transmitted across the network is called stringification.