The Wayback Machine - http://web.archive.org/web/20240614002748/https://www.geeksforgeeks.org/javascript-json-stringify-method/
Open In App

JavaScript JSON stringify() Method

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • value: It is the value that is to be converted into a JSON string.
  • replacer: It is an optional parameter. This parameter value can be an altering function or an array used as a selected filter for the stringify. If the value is empty or null then all properties of an object are included in a string.
  • space: It is also an optional parameter. This argument is used to control spacing in the final string generated using the JSON.stringify() function. It can be a number or a string if it is a number then the specified number of spaces are indented to the final string and if it is a string then that string is (up to 10 characters) used for indentation.

Return Value: Returns a string for a given value.

JavaScript JSON stringify() Method Examples

Example 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.

Javascript
const value = {
    Company: "GeeksforGeeks",
    Estd: 2009,
    location: "Noida"
};
const result = JSON.stringify(value);
console.log("value of result = " + result);

Output
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.

Javascript
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);

Output
{
  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.

Javascript
let value = ["Logan", 21, "Peter", 24];
let result = JSON.stringify(value);
console.log("value of result = " + result);
console.log("type of result = " + typeof result);

Output
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:

  • Chrome 4.0
  • Firefox 3.5
  • Microsoft Edge 12.0
  • Opera 11.0
  • Internet Explorer 8.0
  • Safari 4.0

Previous Article
Next Article

Similar Reads

How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development. JSON.parse() MethodJSON.parse() converts a JSON string to a JavaScrip
2 min read
How to JSON Stringify an Array of Objects in JavaScript ?
In JavaScript, the array of objects can be JSON stringified for easy data interchange and storage, enabling handling and transmission of structured data. The below approaches can be utilized to JSON stringify an array of objects. Table of Content Using JSON.stringify with a Replacer Function Using a Custom Function for JSON StringifyUsing JSON.stri
2 min read
Node.js querystring.stringify() Method
The querystring.stringify() method is used to produce an URL query string from the given object that contains the key-value pairs. The method iterates through the object's own properties to generate the query string. It can serialize a single or an array of strings, numbers, and booleans. Any other types of values are coerced to empty strings. Duri
2 min read
Convert JSON String to Array of JSON Objects in JavaScript
Given a JSON string, the task is to convert it into an array of JSON objects in JavaScript. This array will contain the values derived from the JSON string using JavaScript. There are two approaches to solve this problem, which are discussed below: Approach 1: Using JSON.parse() MethodThe basic method to convert JSON String to Array of JSON object
2 min read
JSON Modify an Array Value of a JSON Object
The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types: nullbooleannumberstringarrayobject The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modi
2 min read
How to use cURL to Get JSON Data and Decode JSON Data in PHP ?
In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP. cURL: It stands for Client URL.It is a command line tool for sending and getting files using URL syntax.cURL allows communicating with other servers using HTTP, FTP, Telnet, and more. Approach: We are going to fetch JSON data from one of free website,
2 min read
How to transforms JSON object to pretty-printed JSON using Angular Pipe ?
JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. In this article, we will see How to use pipe to transform a JSON object to pretty-printed JSON using Angular Pipe. It meaning that it will take a JSON object string and return it pretty-p
3 min read
Difference between package.json and package-lock.json files
In this article, we will learn the major differences between package.json and package.lock.json and their needs in Node. In Node, package.json is a versioning file used to install multiple packages in your project. As you initialize your node application, you will see three files installed in your app that is node_modules, package.json, and package
3 min read
How to Write Postman Test to Compare the Response JSON against another JSON?
In the era of API-driven development, testing the API responses to ensure their correctness is of paramount importance. One such test includes comparing the JSON response of an API against a predefined JSON. In this article, we will delve into how to write a Postman test for this purpose. Table of Content What is Postman?JSON and its Role in APIsWh
4 min read