Open In App

Convert JSON String to Array of JSON Objects in JavaScript

Last Updated : 21 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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.`);
});

Output
Aditi 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.


Next Article

Similar Reads