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
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Top 10 Projects For Beginners To Practice HTML and CSS Skills Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis
8 min read