.stringify()
The .stringify()
method returns a string representation of a given JSON object. It is one of two methods that belong to the reserved JSON
object, the other being the .parse()
method.
Syntax
JSON.stringify(jsonObject);
The jsonObject
must be valid. Otherwise, a SyntaxError
is thrown.
Example
In the example below, a valid JSON object is stored in a variable, myJSONObject
, and then passed into the .stringify()
method as an argument:
let myJSONObject = {Hello: 'World',};console.log(JSON.stringify(myJSONObject));
The output will look like this:
{"Hello":"World"}
Codebyte Example
The following example further demonstrates how the .stringify()
method works: