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 in JavaScript.

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:

Convert Array to JSON Object JavaScript

Let’s use the following method to convert array, array object and two dimensional array to json object in JavaScript:

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

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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