Convert Array to JSON Object JavaScript

[ad_1]

In js, JSON.stringify() function helps us to convert an object, array, or value to a JSON string in js; In this tutorial, we will show you how to convert an array to a JSON object in JavaScript.

Ezoic

If you are working with JSON in JavaScript, then you should also read these two posts. Which is about JSON of javascript:

How to Convert Array to JSON Object JavaScript

Here are some approaches using JSON.stringify() and JSON.parse() to convert array, array object and two-dimensional array to JSON object in js:

  • 1. Convert Array to JSON Object JavaScript
  • 2. Converting an Object to an Array
  • 3. Convert two dimensional ( 2d ) arrays to JSON Object JavaScript

1. Convert Array to JSON Object JavaScript

You can use JSON.stringify to convert a 1d array into a JSON-formatted string in JavaScript.

Ezoic

Here is an example function to convert array to JSON Object in js.

var array = [1, 2, 3, 4]; 
var arrayToString = JSON.stringify(Object.assign({}, array));  // convert array to string
var stringToJsonObject = JSON.parse(arrayToString);  // convert string to json object
console.log(stringToJsonObject);

Here,

  • 1.JSON.stringify() and Object.assign() method convert array to JSON string.Ezoic
  • 2.JSON.parse() method convert string to JSON object in javascript.

2. Converting an Object to an Array

If you have object array, then you can use Object.entries() function to convert our object to an array of arrays.

Here is an example function:

var object = {
   "first_name": "Test",
   "last_name": "Test",
   "email": "[email protected]"
}
var arr = Object.entries(object);
console.log(arr);

3. Convert two-dimensional ( 2d ) arrays to JSON Object JavaScript

If you have a 2D array, where each sub-array represents a key-value pair; as follows:

var arr = [
    ["Status", "Name", "Marks", "Position"], 
    ["active", "Akash", 10.0, "Web Developer"],
    ["active", "Vikash", 10.0, "Front-end-dev"],
    ["deactive", "Manish", 10.0, "designer"],
    ["active", "Kapil", 10.0, "JavaScript developer"],
    ["active", "Manoj", 10.0, "Angular developer"],
];

Here is an example function that can convert it to a JSON object as follows:

//array.
var arr = [
    ["Status", "Name", "Marks", "Position"], 
    ["active", "Akash", 10.0, "Web Developer"],
    ["active", "Vikash", 10.0, "Front-end-dev"],
    ["deactive", "Manish", 10.0, "designer"],
    ["active", "Kapil", 10.0, "JavaScript developer"],
    ["active", "Manoj", 10.0, "Angular developer"],
];
//javascript create JSON object from two dimensional Array
function arrayToJSONObject (arr){
    //header
    var keys = arr[0];
    //vacate keys from main array
    var newArr = arr.slice(1, arr.length);
    var formatted = [],
    data = newArr,
    cols = keys,
    l = cols.length;
    for (var i=0; i<data.length; i++) {
            var d = data[i],
                    o = {};
            for (var j=0; j<l; j++)
                    o[cols[j]] = d[j];
            formatted.push(o);
    }
    return formatted;
}

Recommended JavaScript Tutorials

EzoicEzoic

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
Jassweb, Rai Chak, Punjab, India. 143518