JSON Functions in JavaScript —
Imagine you are a JavaScript learner building a small website. You want to send a user’s information to a server and receive information from an API. How can you convert JavaScript data into a format that can be sent and read easily?
Meet your new friend: JSON!
Add to Favorites
1. What is JSON?
JSON stands for JavaScript Object Notation. It is a text format used to store and exchange data.
Imagine you have a student named Raveena. You want to store her details:
JavaScript
const student = {
name: "Raveena",
age: 18,
subject: "JavaScript"
};This is a JavaScript object. JSON helps you convert data into a text format that different applications can exchange.
2. The two main JSON functions
Add to Favorites
A. JSON.stringify() — Object to String
Think of it as a packing machine. It packs a JavaScript object into a JSON string.
JavaScript
const student = { name: "Raveena" };const text = JSON.stringify(student);console.log(text);
// {"name":"Raveena"}
Add to Favorites
B. JSON.parse() — String to Object
Think of it as an unpacking machine. It converts valid JSON text back into a JavaScript value.
JavaScript
const text = '{"name":"Raveena"}';const student = JSON.parse(text);console.log(student.name);
// Raveena
3. Story diagram: How JSON works
JavaScript Object
Get Raveenanagar’s stories in your inbox
Join Medium for free to get updates from this writer.
{ name: "Raveena" }
JSON.stringify()
Converts object into text
JSON String
'{"name":"Raveena"}'
JSON.parse()
Converts text back into an object
4. Other useful JSON features
JSON.stringify()with a replacer: lets you choose which properties to include.JSON.parse()with a reviver: lets you transform values while parsing.
Example of selecting properties:
JavaScript
const student = {
name: "Raveena",
age: 18,
city: "Kota"
};const result = JSON.stringify(student, ["name", "city"]);console.log(result);
// {"name":"Raveena","city":"Kota"}
5. Try it yourself!
Question: You have a JSON string and want to convert it into a JavaScript object. Which function will you use?
A. JSON.stringify()
B. JSON.parse()
C. console.log()
Check answer
Remember the story
JSON.stringify()= Pack an object into a string.JSON.parse()= Unpack a JSON string into a value.- JSON is commonly used when exchanging data between websites, servers, and APIs.
Small functions, big possibilities! 🚀







