Converting JavaScript Object to JSON

To convert a JavaScript object to JSON, you can use the JSON.stringify(value, replacer, space) method. The JSON.stringify() method serializes objects, arrays, and primitive values into a JSON data string. The optional "replacer" parameter is a function that allows you to alter the behavior of the serialization process, and the optional "space" parameter allows you to create human-readable JSON strings. If you don't pass a "space" parameter, the JSON.stringify() method will output a minified JSON. In this JavaScript Object to JSON example, we serialize the JavaScript object to JSON data string and pretty-print the generated JSON. Click Execute to run the JavaScript Object to JSON Example online and see the result.
Converting JavaScript Object to JSON Execute
let obj = {
  Client: "Alice",
  City: "London",
  Interests: ["football", "hiking", "gym"]
};

console.log('JavaScript Object to JSON conversion result:');
console.log(JSON.stringify(obj, null, 2))
Updated: Viewed: 17071 times

What is JSON?

JavaScript Object Notation (JSON) is a language-independent text format for holding and exchanging data. Developers use JSON to exchange data between a web browser and a server and exchange data between servers via REST API. There are ready-made code libraries for creating and manipulating JSON data for many programming languages, including JavaScript, Java, C ++, C #, Go, PHP, and Python. JSON file names use the .json file extension.

JSON Example
{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}

How to convert a JavaScript object to a JSON string?

The JSON.stringify(value, replacer, space) method in JavaScript converts objects and arrays into a JSON string. Depending on the requirements, JSON.stringify(value, replacer, space) can produce minified or pretty JSON strings. Minified JSON is needed when storing or sending data over the network, and pretty JSON is required when analyzing serialized objects by humans. Below is an example of converting a JavaScript object to JSON:

Convert JavaScript Object to JSON Example
const obj = {
  id: 2,
  customer: "Jack",
  city: "Paris"
};

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

// output: {"id":2,"customer":"Jack","city":"Paris"}

Converting JavaScript Object to JSON Examples

The following are examples of converting an object to a JSON string in JavaScript:

Converting JavaScript Array to JSON

You can use the JSON.stringify() method to convert a JavaScript array to a JSON data string. The following is an example of converting an array to JSON in JavaScript:

Conver JavaScript Array to JSON Example
const array = ['Earth', 'Mars', 'Venus', 'Jupiter'];
 
console.log(JSON.stringify(array));

// output: ["Earth","Mars","Venus","Jupiter"]

Converting JavaScript Date object to JSON

The following is an example of converting a JavaScript Date object to a JSON data string in JavaScript:

Convert JavaScript Date Object to JSON Example
const date = new Date();

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

// output: "2023-01-27T19:21:39.442Z"

How to alter JavaScript object to JSON conversion result?

When converting a JavaScript object to JSON with the JSON.stringify(value, replacer, space) method, you can use the "replacer" function to alter the resulting JSON. For example, the "replacer" function can replace one value with another according to your conditions:

Convert Object to JSON with "replacer" function
const obj = {Id: 1, Customer: "Jason Sweet"};

function replacer (key, value) {
  return key == 'Id' ? 123 : value;
}

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

// output: {"Id":123,"Customer":"Jason Sweet"}

How to filter JavaScript object to JSON conversion result?

You can pass an array to the "replacer" parameter to filter the result JSON for only specific properties:

Convert Object to JSON with "replacer" array
const obj = {
  name: "Nicolas",
  age: 45,
  city: "London",
};

console.log(JSON.stringify(obj, ["name", "age"]));

// output: {"name":"Nicolas","age":45}

Converting a JavaScript object to a pretty JSON string

To convert a JavaScript object to a pretty JSON string in JavaScript, you can use the third parameter of the JSON.stringify(value, replacer, space) method. Objects serialized in JSON have no padding or extra spaces by default, and you can change this behavior with the third argument.

Convert JavaScript Object to Pretty JSON String Example
let obj = {Id: 6, Name: "Stacy", Rating: "Professional"};

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

// output: 
// {
//   "Id": 6,
//   "Name": "Stacy",
//   "Rating": "Professional"
// }

Converting an object to a custom JSON

You can also convert an object to a custom JSON string using the third parameter of the JSON.stringify(value, replacer, space) method. In this example, we use the "space" parameter to add break characters to the output JSON string:

Convert JavaScript Object to Custom JSON Example
let user = {
  id: 1,
  name: 'Leo',
  status: 'admin',
}

console.log(JSON.stringify(user, null, '->'))

// output: 
// {
//   ->"id": 1,
//   ->"name": "Leo",
//   ->"status": "admin"
// }

How to convert JSON string to object in JavaScript?

To convert a JSON string to an object in JavaScript, you can use the JSON.parse() method:

Convert JSON string to Object Example
let obj = '{"name":"Joe","age":65, "city":"London"}';

obj = JSON.parse(obj);

console.log(obj.city)

// output: London

See also