Convert an Array to JSON in JavaScript Last Updated : 07 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Given a JavaScript Array and the task is to convert an array to JSON Object. Below are the approaches to convert an array to JSON using JsvaScript: Table of ContentJSON.stringify() methodObject.assign() methodJSON.stringify() methodThe use of JSON is to exchange data to/from a web server. While sending data to a web server, the data need to be a string. This method converts the javascript Object(array in this case) into JSON_string. Syntax:JSON.stringify(Javascript Object)Example 1: This example converts the JS array to JSON String using JSON.stringify() method. html <!DOCTYPE html> <html> <head> <title> JavaScript | Convert array to JSON. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 16px;"> </p> <button onclick="gfg_Run()"> Convert </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> let el_up = document.getElementById("GFG_UP"); let el_down = document.getElementById("GFG_DOWN"); let array = [34, 24, 31, 48]; el_up.innerHTML = "Array = [" + array + "]";; function gfg_Run() { el_down.innerHTML = "JSON_String = '" + JSON.stringify(array) + "'"; } </script> </body> </html> Output: Object.assign() method This method copies the values of all properties owned by enumerables from source objects(one or more) to a target object. Syntax:Object.assign(target, ...sources)Example: This example converts the JS array to JSON Object using Object.assign() method. html <!DOCTYPE html> <html> <head> <title> JavaScript | Convert array to JSON. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 16px;"> </p> <button onclick="gfg_Run()"> Convert </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> let el_up = document.getElementById("GFG_UP"); let el_down = document.getElementById("GFG_DOWN"); let array = [34, 24, 31, 48]; el_up.innerHTML = "Array = [" + array + "]";; function gfg_Run() { el_down.innerHTML = "JSON Object = " + JSON.stringify(Object.assign({}, array)); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article Convert an Array to JSON in JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JSON Similar Reads Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen 7 min read JavaScript- Convert an Object to JS Array Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje 3 min read JavaScript - Convert String to Array Strings in JavaScript are immutable (cannot be changed directly). However, arrays are mutable, allowing you to perform operations such as adding, removing, or modifying elements. Converting a string to an array makes it easier to:Access individual characters or substrings.Perform array operations su 5 min read JavaScript - Convert an Array to an Object These are the following ways to convert an array to an Object:1. Using JavaScript Object.assign() method The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.JavaScriptlet a = [1, 2, 3, 2 min read How to Convert JSON to ArrayBuffer in JavaScript? An ArrayBuffer is a complex data type, and structures which has a fixed length and takes binary content as the whole. Any variable that contains pure binary data will be defined in JavaScript as Simple Data, however on some occasions it is sometimes necessary to convert JSON data to an ArrayBuffer, 4 min read How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such 4 min read How to Convert an Array of Objects to Map in JavaScript? Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon 3 min read How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods 6 min read How to Convert JSON to Blob in JavaScript ? This article explores how to convert a JavaScript Object Notation (JSON) object into a Blob object in JavaScript. Blobs represent raw data, similar to files, and can be useful for various tasks like downloading or processing JSON data. What is JSON and Blob?JSON (JavaScript Object Notation): A light 2 min read How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i 3 min read Like