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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay