Convert Array to JSON Object JavaScript

Convert Array to JSON Object JavaScript

In this tutorial, you will learn how to convert array to JSON object.

What is JSON?

JSON means JavaScript Object Notation. JSON is an extremely lightweight data-interchange format for data exchange between server-side and client side which is quick and easy to parse and generate.

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

1. Convert Array to JSON Object JavaScript

You can use JSON.stringify to convert an array into a JSON formatted string in JavaScript.

Suppose there is an array such as “[1, 2, 3, 4]”. If you want to convert this array to JSON Object in javascript. Let’s see the example below

Ex:-

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.
  • 2.JSON.parse() method convert string to JSON object in javascript.

2. Converting an Object to an Array

When converting an object to an array, we’ll use the .entries() method from the Object class. This will convert our object to an array of arrays. Each nested array is a two-value list where the first item is the key and the second item is the value.

Ex:-

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

Suppose there is an array such as

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"],
];

If you want to convert this array to JSON Object in javascript. Let’s see the example below :

Ex:-

//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;
}

You may like

  1. Convert JSON String to JSON Object JavaScript
  2. JavaScript Convert JSON Object to Object String

Leave a Reply

Your email address will not be published. Required fields are marked *