Converting JavaScript Array to JSON

To convert a JavaScript array to a JSON data string, you can use the JSON.stringify(value, replacer, space) method. The optional replacer parameter is a function that can alter the behavior of the serialization process. And the optional space parameter is usually used to beautify the resulting JSON by specifying the white space in the output JSON. The JSON.stringify() method serializes arrays, objects, and primitive values into a JSON string. In this JavaScript Array to JSON example, we serialize a JavaScript array into a JSON string using JSON.stringify() method. Click Execute to run the JavaScript Array to JSON Example online and see the result.
Converting JavaScript Array to JSON Execute
let array = ['Apple', 'Orange', 'Cherry', 'Blueberry'];
 
console.log(JSON.stringify(array));
Updated: Viewed: 9363 times

What is JSON?

JavaScript Object Notation (JSON) is a lightweight text format for storing structured data in a human-readable format. Compared to XML, JSON data takes up less space, is easier for humans to read, and for computers to process. JSON is frequently used to transfer data over the network in client/server and server/server communications. JSON itself has no methods and does not support comments; it's just a data storage format. JSON format came from JavaScript, but it is now actively used in almost all programming languages, including PHP, Python, Java, C++, C#, Go, and most of them, like Python, has built-in modules for working with JSON data.

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

Converting a JavaScript Array to JSON using the JSON.stringify() method

The JSON.stringify(value, replacer, space) method in JavaScript converts arrays and objects to a JSON string. This is necessary when sending data over the network using XMLHttpRequest and the Fetch API (or other communication libraries). Below is an example of converting an array to JSON with the JSON.stringify() method:

Conver JavaScript Array to JSON Example
let array = ['Paris', 'Madrid', 'Rome', 'London'];
 
console.log(JSON.stringify(array));

// output: ["Paris","Madrid","Rome","London"]

Converting a JavaScript Array to a pretty JSON string

JSON.stringify() generates a minified JSON string by default, which takes up less space but is hard to read by humans. To generate a pretty JSON string, the third parameter of the JSON.stringify() method should be used. This parameter determines the number of spaces that should be placed within a JSON string when generating it. By default, padding and extra space for JSON objects are 0 bytes. It is useful when we need to transmit a JSON string over the network, and the data size is small. The third space is used exclusively for human-readable output.

Conver JavaScript Array to pretty JSON string Example
let array = ['Mango', 'Grape', {"Customer": "Leo","City": "Rome"}];
 
console.log(JSON.stringify(array, null, 2));

// output: 
// [
//   "Mango",
//   "Grape",
//   {
//     "Customer": "Leo",
//     "City": "Rome"
//   }
// ]

How to convert JSON string to array in JavaScript?

The first step to converting a JSON string into an array in JavaScript is to convert the string into a JavaScript object using the JSON.parse() method, extracting the values of the object and pushing them into an array using the array.push() method.

Convert JSON string to JavaScript Array Example
let user = '{"name": "Jason", "lastname": "Cuper", "city": "Rome"}';

function myArr() {
  user = JSON.parse(user);
  let arr = [];
              
  for(let i in user)
    arr.push(user[i]);

  return arr
}

console.log(myArr())

// output: ['Jason', 'Cuper', 'Rome']

See also