Stringify JavaScript Object to JSON String

You can use the JSON.stringify(value, replacer, space) method to stringify a JavaScript object or value to a JSON string. The JSON.stringify() method serializes (converts to a JSON string) objects, arrays, and primitive values. The JSON.stringify() method takes three parameters. The first parameter is the object to be converted to JSON. The second optional parameter is a function that controls the behavior of the serialization process. The third optional parameter controls the spacing in the generated JSON string. In this JavaScript JSON Stringify example, we use the JSON.stringify() method to convert a JavaScript object to a JSON string. Click Execute to run the JavaScript JSON Stringify Example online and see the result.
Stringify JavaScript Object to JSON String Execute
const obj = {
  Id: 78912,
  Customer: "Jason Sweet",
  City: "Rome"
};

console.log(JSON.stringify(obj));
Updated: Viewed: 7145 times

What is JSON?

JavaScript Object Notation (JSON) is a text format for representing structured data based on the syntax of the JavaScript language. JSON file names use the .json file extension. JSON is usually used for data exchange between applications written in many programming languages, including JavaScript, Python, PHP, C++, Java, C#, Go, and many more.

JSON supports the following data types:

  • Objects {...}
  • Arrays [...]
  • Primitives: string, numbers, boolean values ​​true/false, null

JavaScript JSON.stringify() Method

The JSON.stringify(value, replacer, space) method converts JavaScript objects to a JSON string. The resulting JSON string is a JSON-formatted or serialized object that can be sent over the network or stored on a disk. You can restore or deserialize a JavaScript object from a JSON data string using the JSON.parse(text, reviver) method. Note that a JSON object has several important differences from literal objects: strings only use double quotes; object property names are enclosed in double-quotes.

JSON.stringify() Syntax

The following is a syntax for the JSON.stringify() method:

JavaScript JSON.stringify() Syntax
JSON.stringify(value, replacer, space)

Where:
  • value: a JavaScript object or value
  • replacer (optional): a function that changes the behavior of the serialization process or an array of property names to be serialized
  • space (optional): a string or numeric value used to insert spaces into the output JSON string for readability
JSON.stringify() Example

The following is a basic JSON.stringify() example:

JavaScript JSON.stringify() Example
const obj = {
  Spain: "Madrid",
  Italy: "Rome",
  Portugal: "Lisbon"
};

console.log(JSON.stringify(obj));

// output: {"Spain":"Madrid","Italy":"Rome","Portugal":"Lisbon"}

JavaScript JSON.stringify() Examples

The following are examples of using the JSON.stringify() method in JavaScript:

Customizing the JSON.stringify() process

In most cases, the JSON.stringify() method is only used with the default parameters. If you need to customize the serialization process, such as filtering out circular references, you can do so with a "replacer" array that can be passed as the second argument to the JSON.stringify() method. For example, if you pass in an array containing property names, only those properties will be stringified.

JavaScript JSON.stringify() Customizing Example
const obj = {
  Id: 78912,
  Customer: [{name: "Jason"}, {surname: "Sweet"}],
};

console.log(JSON.stringify(obj, ['Id']))

// output: {"Id":78912}

Creating a pretty JSON string using JSON.stringify()

The third parameter of the JSON.stringify(value, replacer, space) method is the number of spaces used for convenient formatting. Objects serialized in JSON format have no indentation or extra space by default. This is useful if you send a JSON data string over the network or store it on the disk. The third "space" argument is used solely to produce human-readable output.

Creating a Pretty JSON String Example
const obj = {Id:78912,Customer:"Jason Sweet",City: "Rome"};

console.log(JSON.stringify(obj, null, 2));

// output: 
// {
//   "Id": 78912,
//   "Customer": "Jason Sweet",
//   "City": "Rome"
// }

Creating a JSON String using the toJSON() method

JavaScript objects can provide an object.toJSON() method to control the serialization process. JSON.stringify() will automatically call this method if it exists for the object being serialized. The object.toJSON() method is used when calling JSON.stringify() directly on the object and when the object is nested within another serializable object.

Stringify object to JSON with toJSON() method Example
const age = {
  age: 25,
  toJSON() {
    return this.age + 10;
  }
};

const person = {
  name: "Jason Sweet",
  age
};

console.log(JSON.stringify(age)); 
console.log(JSON.stringify(person));

// output: 35
// output: {"name":"Jason Sweet","age":35}

How to decode JSON string back to JavaScript object?

To decode or parse a JSON data string back into a JavaScript object, you can use the JSON.parse() method:

JavaScript JSON.parse() Example
const json = '{"id":78912,"customer":"Jason Sweet","city":"Rome"}';

const obj = JSON.parse(json);

console.log(obj.customer);

// output: Jason Sweet

See also