Given a JSON string and the task is to convert the JSON string to the array of JSON objects. This array contains the values of JavaScript object obtained from the JSON string with the help of JavaScript. There are two approaches to solve this problem which are discussed below:
Approach 1: First convert the JSON string to the JavaScript object using JSON.Parse() method and then take out the values of the object and push them into the array using push() method.
-
Example:
<!DOCTYPE HTML><html><head><title>How to convert JSON string to arrayof JSON objects using JavaScript?</title></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><pid="GFG_UP"></p><buttononclick="myGFG()">Click Here</button><pid="GFG_DOWN"></p><script>var up = document.getElementById("GFG_UP");var JS_Obj ='{"prop_1":"val_1", "prop_2":"val_2", "prop_3" : "val_3"}';up.innerHTML = "JSON string - '" + JS_Obj + "'";var down = document.getElementById("GFG_DOWN");function myGFG() {var obj = JSON.parse(JS_Obj);var res = [];for(var i in obj)res.push(obj[i]);down.innerHTML = "Array of values - ["+ res + "]";}</script></body></html>chevron_rightfilter_none -
Output:
Approach 2: This approach is also the same but using a different method. Convert the JSON string to the JavaScript object using eval() method and then take out the values of the object and push them to the array using push() method.
-
Example:
<!DOCTYPE HTML><html><head><title>How to convert JSON string to arrayof JSON objects using JavaScript?</title></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><pid="GFG_UP"></p><buttononclick="myGFG()">Click Here</button><pid="GFG_DOWN"></p><script>var up = document.getElementById("GFG_UP");var JS_Obj ='{"prop_1":"val_1", "prop_2":"val_2", "prop_3" : "val_3"}';up.innerHTML = "JSON string - '" + JS_Obj + "'";var down = document.getElementById("GFG_DOWN");function myGFG() {var obj = eval('(' + JS_Obj + ')');var res = [];for(var i in obj)res.push(obj[i]);down.innerHTML = "Array of values - ["+ res + "]";}</script></body></html>chevron_rightfilter_none -
Output:
Recommended Posts:
- Extract unique objects by attribute from array of objects.
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- How to convert JSON data to a html table using JavaScript/jQuery ?
- How to convert JSON results into a date using JavaScript ?
- JSON | modify an array value of a JSON object
- JavaScript | Convert an array to JSON
- How to convert PHP array to JavaScript or JSON ?
- JavaScript | Check if a string is a valid JSON string
- Sort array of objects by string property value in JavaScript
- How to convert Integer array to String array using JavaScript ?
- Sort an array of objects using Boolean property in JavaScript
- How to remove duplicates from an array of objects using JavaScript ?
- How to remove object from array of objects using JavaScript ?
- JavaScript Course | Objects in JavaScript
- Convert comma separated string to array using JavaScript
- How to check two objects have same data using JavaScript ?
- How to pretty print JSON string in JavaScript ?
- Max/Min value of an attribute in an array of objects in JavaScript
- How to remove Objects from Associative Array in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


