| by Arround The Web | No comments

Convert Array to JSON – JavaScript

While managing the data in JavaScript, there can be a requirement to sort the data effectively to make it readable. For instance, associating the inter-related data to make it useful. In such case scenarios, converting an array to JSON assists in organizing and utilizing the data effectively on the developer’s end.

This write-up will illustrate the approaches to transform an array into JSON in JavaScript.

How to Convert/Transform an Array to JSON Using JavaScript?

To convert the array into a JSON object, apply the following methods in combination with the “JSON.stringify()” method:

Method 1: Convert Array to JSON Via JSON.stringify() and JSON.parse() Methods in JavaScript

The “JSON.stringify()” method transforms a JavaScript object into a string, and the “JSON.parse()” method converts text to a JavaScript object. These methods can be utilized to transform the integers array into an object array.

Syntax

JSON.stringify(arr)

In the above syntax, “arr” refers to the array that needs to be converted into a string.

JSON.parse(text, receiver)

In this syntax:

  • text” indicates the string value that needs to be parsed into JSON.
  • receiver” is an optional parameter to parse the function.

Example

Let’s overview the below-stated code lines:

<script>

let myArray = [1, 2, 3, 4, 5];

console.log('The given Array is this : ' + myArray);

let jsonObj = JSON.parse(JSON.stringify(myArray));

console.log('This is converted JSON ' + typeof(jsonObj));

console.log(jsonObj);

</script>

In the above code block:

  • Firstly, create an array named “myArray” and display its values on the console.
  • In the next step, apply the “JSON.stringify()” method that converts the specified array into JSON string format.
  • After that, convert the transformed JSON string values into an object using the “JSON.parse()” method.
  • Lastly, analyze the type of the transformed value with the help of the “typeof” operator and display the converted object on the console.

Output

In the above-given output, it is evident that the given array is converted into an object.

Method 2: Convert an Array to JSON Via JSON.stringify() and Object.assign() Methods in JavaScript

The “Object.assign()” method is used to place the values from one or more than one source objects into a target object. This method can be implemented combined with the “JSON.stringify()” method to transform the given string values in an array into target object values.

Syntax

Object.assign(target, ...sources)

In the above-given syntax:

  • target” points to the target object.
  • sources” correspond to the properties to be applied.

Example

Let’s go through the following code lines:

<script>

let myArray = ['JavaScript',' HTML', ' CSS', ' Bootstrap']

console.log('The given array is this : ' + myArray);

let jsonObj = JSON.stringify(Object.assign({}, myArray))

console.log('Converted JSON Object is this : ' + jsonObj);

</script>

In the above code snippet:

  • Similarly, create an array named “myArray” having the stated string values and display it.
  • In the next step, apply the “Object.assign()” method that appends all the enumerable entities of the provided array in the form of an object.
  • After that, likewise, apply the “JSON.stringify()” method to convert the resultant object into the string format.
  • Finally, display the resultant JSON object on the console.

Output

It can be observed that firstly the array is converted into an object and then displayed as a string.

Conclusion

To convert/transform the array into a JSON object, apply the “JSON.stringify()” method in combination with the “JSON.parse()” method or the “Object.assign()” method. These methods can be utilized to parse the integers and strings array into an object, respectively. This article discussed the approaches to transform an array to JSON in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply