Last Updated : 11 Jul, 2025
The JSON.stringify()
method in JavaScript is used to convert JavaScript objects into a JSON string. This method takes a JavaScript object as input and returns a JSON-formatted string representing that object.
Syntax:
JSON.stringify(value, replacer, space);Parameters:
Return Value: Returns a string for a given value.
JavaScript JSON stringify() Method ExamplesExample 1: Converting JavaScript Object to JSON String
The code demonstrates how to convert a JavaScript object obj
into a JSON string using JSON.stringify()
. The resulting JSON string represents the properties of the object in a serialized format.
const value = {
Company: "GeeksforGeeks",
Estd: 2009,
location: "Noida"
};
const result = JSON.stringify(value);
console.log("value of result = " + result);
value of result = {"Company":"GeeksforGeeks","Estd":2009,"location":"Noida"}
Example 2: Deep Copying JavaScript Object with JSON.stringify() and JSON.parse()
The code creates an object obj
with nested properties. JSON.stringify()
converts obj
to a JSON string, then JSON.parse()
parses it back to an object obj2
. Modifying obj2
doesn't affect obj
, illustrating deep copying.
let obj = {
name: "GFG",
add: {
country: "India",
state: {
code: "JS",
topic: "stringify"
}
}
}
let obj2 = JSON.parse(JSON.stringify(obj));
obj2.add.state.topic = "stringify json object";
console.log(obj);
console.log(obj2);
{ name: 'GFG', add: { country: 'India', state: { code: 'JS', topic: 'stringify' } } } { name: 'GFG', add: { country: 'India', state: { code: 'JS', topic: 'stringify json object' } } ...
Example 3: Converting Array to JSON String
The code converts the array value
into a JSON string using JSON.stringify()
. The resulting string result
is logged along with its type. This demonstrates how JSON.stringify() converts JavaScript data types into JSON strings.
let value = ["Logan", 21, "Peter", 24];
let result = JSON.stringify(value);
console.log("value of result = " + result);
console.log("type of result = " + typeof result);
value of result = ["Logan",21,"Peter",24] type of result = string
We have a complete list of Javascript JSON methods, to check those please go through Javascript JSON Complete Reference article.
Supported browsers:RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4