Convert JSON String to Array of JSON Objects in JavaScript
Last Updated :
21 Jun, 2025
Converting a JSON string to an array of JSON objects in JavaScript involves transforming a structured text format (JSON) into a usable JavaScript array. This allows developers to work directly with the data, enabling easier manipulation, analysis, and display of information.
Below are some common methods that are used to convert JSON strings to array of JSON objects:
1. Using JSON.parse() Method
Using the JSON.parse() method, a JSON string can be easily converted into an array of JSON objects in JavaScript. This method interprets the string and returns a JavaScript array, enabling developers to work directly with the structured data programmatically.
JavaScript
const jsonStr = '[{"name": "Aditi", "age": 30}, {"name": "Devesh", "age": 25}, {"name": "Esha", "age": 35}]';
// Parse JSON string to array of objects
const jsonArray = JSON.parse(jsonStr);
// Output the array of objects
console.log(jsonArray);
Output[
{ name: 'Aditi', age: 30 },
{ name: 'Devesh', age: 25 },
{ name: 'Esha', age: 35 }
]
2. Using JavaScript eval() Method
The eval() method can technically be used to convert a JSON string into an array of objects, it is not recommended due to security risks. Using eval() allows the execution of arbitrary JavaScript code, which can lead to vulnerabilities if the JSON string contains malicious code.
JavaScript
const jsonStr = '[{"name": "Adams", "age": 30}, {"name": "Davis", "age": 25}, {"name": "Evans", "age": 35}]';
let obj = eval('(' + jsonStr + ')');
let res = [];
for (let i in obj)
res.push(obj[i]);
console.log(res);
Output[
{ name: 'Adams', age: 30 },
{ name: 'Davis', age: 25 },
{ name: 'Evans', age: 35 }
]
3. Using map()
The JSON string may represent a more complex structure that includes nested objects or arrays. In such cases, we can use the map() method after parsing to transform or filter the data into the desired format.
JavaScript
const jsonStr = '[{"name": "Jiya", "age": 30, "city": "Agra"}, {"name": "Jolly", "age": 25, "city": "Delhi"}]';
const jsonArr= JSON.parse(jsonStr);
const newArr = jsonArr.map(person => {
return { name: person.name, city: person.city };
});
console.log(newArr);
Output[ { name: 'Jiya', city: 'Agra' }, { name: 'Jolly', city: 'Delhi' } ]
4. Using forEach Loop
The forEach() method is a cleaner, more modern way to iterate over arrays in JavaScript. It simplifies the syntax and allows you to define a callback function to execute for each item in the array.
JavaScript
const jsonStr = '[{"name": "Aditi", "age": 30}, {"name": "Devesh", "age": 25}, {"name": "Esha", "age": 35}]';
const jsonArr = JSON.parse(jsonStr);
jsonArr.forEach(person => {
console.log(`${person.name} is ${person.age} years old.`);
});
OutputAditi is 30 years old.
Devesh is 25 years old.
Esha is 35 years old.
5. Using a Third-Party Library
While JSON.parse() is a native method in JavaScript, sometimes we may need additional capabilities, such as better handling of edge cases (e.g., comments in JSON). Third-party libraries like json5 or lodash can assist in parsing and converting JSON strings more effectively.
JavaScript
// First, include json5.js library (this requires npm or a script tag)
const JSON5 = require('json5');
const jsonString = '[/* Comment */ {"name": "Jiya", "age": 30}]';
// Parse the JSON5 string into an array
const jsonArray = JSON5.parse(jsonString);
console.log(jsonArray);
Output
[
{ name: 'Jiya', age: 30 }
]
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
Conclusion
Converting a JSON string to an array of JSON objects in JavaScript is key for data manipulation. Methods like JSON.parse(), map(), forEach(), and for make it easy to work with JSON data, while alternatives like eval() and third-party libraries provide additional flexibility when needed.
Similar Reads
How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i
3 min read
How to Convert String to Array of Objects JavaScript ? Given a string, the task is to convert the given string to an array of objects using JavaScript. It is a common task, especially when working with JSON data received from a server or API. Below are the methods that allow us to convert string to an array of objects:Table of ContentUsing JSON.parse()
4 min read
How to JSON Stringify an Array of Objects in JavaScript ? In JavaScript, the array of objects can be JSON stringified for easy data interchange and storage, enabling handling and transmission of structured data. The below approaches can be utilized to JSON stringify an array of objects.Table of ContentUsing JSON.stringify with a Replacer Function Using a C
3 min read
How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read
How to Convert JS Object to JSON String in JQuery/Javascript? Converting a JavaScript object to a JSON string means using the JSON.stringify() method to transform the object into a JSON-formatted string. This allows for efficient data storage, transmission, and debugging by representing complex data structures in a standardized text format.To Convert JS Object
4 min read
Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen
7 min read