Convert Array to JSON Object JavaScript

Convert Array to JSON Object JavaScript

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.

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.

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.
  • 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

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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