node json stringify
const obj = {
name: 'John',
age: 30,
city: 'New York'
};
const jsonString = JSON.stringify(obj);
console.log(jsonString);
Declare an object named
obj
with three key-value pairs:name
with the value'John'
,age
with the value30
, andcity
with the value'New York'
.Use
JSON.stringify()
to convert theobj
object into a JSON string representation. This method creates a JSON string from the provided object.Store the JSON string generated from the
obj
object in the variablejsonString
.Print the
jsonString
variable to the console usingconsole.log()
. This displays the JSON string representation of theobj
object.