Category: javascript
what is JSON.stringify is a JavaScript
Published on 06 May 2026
Explanation
JSON.stringify() is a JavaScript method
used to
convert a JavaScript object, array, or value
into a JSON string. It is mainly
used when sending data to APIs, storing
data in localStorage, or
converting objects into
transferable string format.
Code:
const user = {
name: 'Praveen',
age: 25
};
const jsonData = JSON.stringify(user);
console.log(jsonData);
// Output: {"name":"Praveen","age":25}
Explanation
It is commonly used while sending data
in API requests because APIs usually accept
JSON string data.
Code:
const data = {
title: 'React Course',
price: 999
};
fetch('https://api.example.com/course', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
Explanation
JSON.stringify() is used to store
objects in
localStorage because
localStorage only stores strings.
Code:
const user = {
name: 'Kumar',
city: 'Chennai'
};
localStorage.setItem('user',
JSON.stringify(user));
Explanation
You can convert arrays into JSON strings
using JSON.stringify().
Code:
const numbers = [1, 2, 3, 4]; const result = JSON.stringify(numbers); console.log(result); // Output: [1,2,3,4]
Explanation
JSON.stringify() can format
JSON output using additional
parameters for readability.
The third parameter adds
indentation spaces.
Code:
const student = {
name: 'Arun',
marks: 90
};
const formatted =
JSON.stringify(student, null, 2);
console.log(formatted);
/* Output:
{
"name": "Arun",
"marks": 90
}
*/
Explanation
Functions, undefined values, and
symbols are not
included in JSON.stringify()
output because JSON format
does not support them.
Code:
const obj = {
name: 'Test',
age: undefined,
greet: function() {
console.log('Hello');
}
};
console.log(JSON.stringify(obj));
// Output: {"name":"Test"}