The Wayback Machine - https://web.archive.org/web/20240717024533/https://www.geeksforgeeks.org/how-to-convert-json-string-to-array-of-json-objects-using-javascript/
Open In App

Convert JSON String to Array of JSON Objects in JavaScript

Last Updated : 28 May, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Given a JSON string, the task is to convert it into an array of JSON objects in JavaScript. This array will contain the values derived from the JSON string using JavaScript. There are two approaches to solve this problem, which are discussed below:

Approach 1: Using JSON.parse() Method

The basic method to convert JSON String to Array of JSON object is by using JSON.parse() method. This method is used to parse a JSON string which is written in a JSON format and returns a JavaScript object.

JavaScript
// JSON String Representing an Array of Objects
const jsonStr = '[{"name": "Adams", "age": 30}, {"name": "Davis", "age": 25}, {"name": "Evans", "age": 35}]';

// Parse JSON string to array of objects
const jsonArray = JSON.parse(jsonStr);

// Output the array of objects
console.log(jsonArray);

Output
[
  { name: 'Adams', age: 30 },
  { name: 'Davis', age: 25 },
  { name: 'Evans', age: 35 }
]

Approach 2: Using JavaScript eval() Method

The eval() method parse a JSON string and then converts it into an array of objects. However, using eval() method is not recommended due to potential security risks, as it can execute arbitrary code.

JavaScript
// JSON String Representing an Array of Objects
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 }
]

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.



Similar Reads

How to Convert Array of Objects into Unique Array of Objects in JavaScript ?
Arrays of objects are a common data structure in JavaScript, often used to store and manipulate collections of related data. However, there are scenarios where you may need to convert an array of objects into a unique array, removing any duplicate objects based on specific criteria. JavaScript has various methods to convert an array of objects into
6 min read
Filter Array of Objects with Another Array of Objects in JavaScript
Filtering an array of objects with another array in JavaScript involves comparing and including objects based on specific criteria. Below are the approaches to filter an array of objects with another array of objects in JavaScript: Table of Content Using filter and includes MethodsUsing LoopingUsing reduce MethodUsing filter and includes MethodsIn
3 min read
How to Remove Null Objects from Nested Array of objects in JavaScript ?
Removing null objects from the nested array of objects can be done by iterating over the array, filtering out the null objects at each level, and then applying a recursive approach to remove all the null objects. This makes sure that all the levels of the nested structure are checked and the null objects are removed. There are various approaches to
6 min read
How to Separate Array of Objects into Multiple Objects in JavaScript ?
In JavaScript, the task of separating an array of objects into multiple objects involves organizing and restructuring data based on specific criteria or properties. This process is often necessary when dealing with datasets that need to be grouped or segmented for easier analysis or manipulation. There are various methods to achieve this separation
4 min read
How to Remove Multiple Objects from Nested Array of Objects in JavaScript ?
A nested array of objects is an array of arrays that contain multiple objects as their elements. Removing multiple objects from the nested array of objects in JavaSript can be accomplished in different ways as listed below: Table of Content Using filter() with some() methodUsing filter() with includes() methodUsing filter( ) with findIndex() method
6 min read
How to Update an Array of Objects with Another Array of Objects using Lodash?
Updating an array of objects with another array of objects involves merging or updating objects in the first array with corresponding objects from the second array. Below are the approaches to update an array of objects with another array of objects in the Lodash library: Table of Content Using _.merge function from lodashUsing _.mergeWith function
2 min read
How to convert CSV string file to a 2D array of objects using JavaScript ?
A CSV is a comma-separated values file with a .csv extension, which allows data to be saved in a tabular format. In this article, we will learn to convert the data of a CSV string to a 2D array of objects, where the first row of the string is the title row using JavaScript. Given a comma-separated values (CSV) string to a 2D array, using Javascript
4 min read
Write a program to convert an array of objects to a CSV string that contains only the columns specified using JavaScript
When we want to store multiple values in one variable then instead of a variable, we use an array. which allows you to store multiple types of data for example you could have a string, an integer, etc. In this article, we will learn to convert the array objects to CSV string data. Given array objects ( key: value ) to CSV string comma-separated val
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 Content Using JSON.parse() MethodUsing String Splitting and Object Construc
4 min read
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 in a file. Table of Content Using JSON.parse() Meth
2 min read
three90RightbarBannerImg