How to convert JSON string to array of JSON objects using JavaScript ? - GeeksforGeeks

30 Jul, 2021
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 array        of JSON objects using JavaScript?    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP"></p>          <button onclick = "myGFG()">        Click Here    </button>          <p id = "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>
<!DOCTYPE HTML> <html>   <head>     <title>         How to convert JSON string to array        of JSON objects using JavaScript?    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP"></p>          <button onclick = "myGFG()">        Click Here    </button>          <p id = "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>
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 array        of JSON objects using JavaScript?    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP"></p>          <button onclick = "myGFG()">        Click Here    </button>          <p id = "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>
<!DOCTYPE HTML> <html>   <head>     <title>         How to convert JSON string to array        of JSON objects using JavaScript?    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP"></p>          <button onclick = "myGFG()">        Click Here    </button>          <p id = "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>
Output:
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.
HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.
CSS-Misc
HTML-Misc
JavaScript-Misc
CSS
HTML
JavaScript
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to apply style to parent if it has child with CSS?
How to set space between the flexbox ?
Design a web page using HTML and CSS
Create a Responsive Navbar using ReactJS
Form validation using jQuery
How to set the default value for an HTML <select> element ?
Hide or show elements in HTML using display property
How to set input type date in dd-mm-yyyy format using HTML ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?