Hi everyone! Here is my another blog from Javascript series. Today we are going to see about JSON.stringify(). Let's dive into it,
First we have to know what is JSON.
What is JSON?
JSON stands for:
JavaScript Object Notation
It’s a simple way to store and share data, like a universal language that different apps and websites understand.
Why is JSON important?
- Because when computers, websites, or apps talk to each other, they exchange data, and JSON is the most common format they use to do it.
- It works everywhere ,in websites, mobile apps, APIs, and databases.
I hope you got better idea about JSON. Now let's see about .stringify().
JSON.stringify()
I will give you a example for better understanding about this,
Imagine this:
You have a box 📦 (object) with information:
let student = {
name: "Sevaki",
age: 22
};
This is a JavaScript object. It’s great inside your code, but you can’t send it directly to a server, or save it in localStorage, because it's not a simple string.
So what do we do?
We need to change the object into a string, and that's what JSON.stringify() does!
let student = {
name: "Sevaki",
age: 22
};
let stringData = JSON.stringify(student);
console.log(stringData);
Output:
{"name":"Sevaki","age":22}
Now it's a string version of the object!
Takeaways:
- JSON is a text format for data
- Commonly used for data transfer (like in APIs)
- Use JSON.stringify() to convert object to string
- Use JSON.parse() to convert string to object
So that's guys. I gave a beginner explanation only. In my future blog I will watering to this blog. Thankyou for reading my blog. Will see you in my next blog. If anything wrong in this blog please do comments, it will helpful for me too.
Top comments (1)
you used stringData variable only one time in your code so you can directly give inside the console.log
let student = {
name: "Sevaki",
age: 22
};
console.log(JSON.stringify(student));
This also give you a same output.