Skip to main content

Convert an Array of JSON Objects to an Array of Objects in JavaScript

It is kind of a common task to convert an array of JSON objects to an array of objects, esp when we get data from an API.

You can use the JSON.parse() method. This method is used to parse a string of JSON data and transform it into a JavaScript object. The string of JSON data must be valid JSON syntax in order for the parse() method to successfully parse it.

To use the JSON.parse() method, simply pass in the string ofJSON data as the first argument.

var jsonArray = [
 '{"name": "Edyth Hodkiewicz", "title": "Prof.", "age": 33,"phone": "1-636-405-1711 x0367", "email": "[email protected]" }',
 '{"name": "Nat Feeney", "title": "Mrs.", "age": 34, "phone": "(313) 679-9226 x25806", "email": "[email protected]" }', 
 '{"name": "Raymond Friesen", "title": "Dr.", "age": 50, "phone": "881.612.1937 x915", "email": "[email protected]" }'
];

//use map() method
var objectsArray = jsonArray.map(JSON.parse);

console.log(objectsArray);
//output: Array [Object { name: "Edyth Hodkiewicz", title: "Prof.", age: 33, phone: "1-636-405-1711 x0367", email: "[email protected]" }, Object { name: "Nat Feeney", title: "Mrs.", age: 34, phone: "(313) 679-9226 x25806", email: "[email protected]" }, Object { name: "Raymond Friesen", title: "Dr.", age: 50, phone: "881.612.1937 x915", email: "[email protected]" }]

//use for each
var objectsArray = [];
for (var i = jsonArray.length - 1; i >= 0; i--){
  objectsArray[i] = JSON.parse(jsonArray[i]);
} 
console.log(objectsArray);

//use join() method
var objectsArray = JSON.parse('[' + jsonArray.join(',') + ']');
console.log(objectsArray);

By continuing to use the site, you agree to the use of cookies.