Convert an Array to JSON in JavaScript Last Updated : 07 Oct, 2024 Summarize Comments Improve Suggest changes Share 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 Here are some common methods which can be used 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.JavaScrip 3 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 Like