DEV Community

_Khojiakbar_
_Khojiakbar_

Posted on • Edited on

JSON

JSON.stringify()

The JSON.stringify() method converts a JavaScript object or value to a JSON string. This is useful for sending data over a network, storing data in text format, or logging.

const obj = { name: "John", age: 30, city: "New York" }; const jsonString = JSON.stringify(obj); console.log(jsonString); // Output: {"name":"John","age":30,"city":"New York"} 
Enter fullscreen mode Exit fullscreen mode

JSON.parse()

The JSON.parse() method parses a JSON string and constructs the JavaScript value or object described by the string. This is useful for converting JSON data received from a web server into JavaScript objects.

const jsonString = '{"name":"John","age":30,"city":"New York"}'; const obj = JSON.parse(jsonString); console.log(obj.name); // Output: John console.log(obj.age); // Output: 30 console.log(obj.city); // Output: New York 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)