Inside JavaScript there is a built-in function JSON.stringify
which converts any variable into json string.
The syntax is:
JSON.stringify(value, replacer, space)
- value - Any variable you want to convert into JSON string
- replacer - This method will be called on each item of the variable
- space - Number of spaces you want to add to each item of the variable
The second and thrird arguments are optional.
Lets have a look at the following example:
const users = [
{
name: 'John',
isActive: false,
},
{
name: 'Jane',
isActive: true,
},
];
console.log(JSON.stringify(users, null, 2));
Upon running above code you will get the following output:
Top comments (1)
)