π©JSON
JavaScript provides methods to work with JSON data:
const person = {
name: "John Doe",
age: 30,
email: "john@example.com",
isSubscribed: true,
hobbies: ["Reading", "Running", "Cooking"],
address: {
city: "New York",
zipCode: "10001",
},
};
// Convert JavaScript object to JSON string
const jsonString = JSON.stringify(person);
console.log(jsonString);
/* Output:
{
"name":"John Doe",
"age":30,
"email":"john@example.com",
"isSubscribed":true,
"hobbies":["Reading","Running","Cooking"],
"address":{"city":"New York","zipCode":"10001"}
}
*/
// Parse JSON string back to JavaScript object
const parsedObject = JSON.parse(jsonString);
console.log(parsedObject.name); // Output: "John Doe"
console.log(parsedObject.hobbies); // Output: ["Reading", "Running", "Cooking"]
Last updated