Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array from JSON - Javascript

I have a JSON file that I would like to create an array from.

Here is the JSON data.

{
  "table": {
    "columnNames": ["column1", "column2", "column3", "column4"],
    "columnTypes": ["String", "String", "String", "String"],
    "rows": [
      ["data00", "data01", "data02", "data03"],
      ["data10", "data11", "data12", "data13"],
      ["data20", "data21", "data22", "data23"],
      ["data30", "data31", "data32", "data33"]
     ]
   }
}

I need to create an array from the objects in the "rows" section.

Any help would be appreciated!

Thanks!!!

EDIT

Would it be possible to create a hash table out of the data in rows? Also, how would you perform JSON.parse on a json file? Thanks

like image 887
gberg927 Avatar asked Nov 10 '11 21:11

gberg927


1 Answers

Do you mean you want to get a single array holding all the values?

var rows = [];

for (var i = 0; i < data.table.rows.length; i++) {
    rows.push.apply(rows, data.table.rows[i]);
}

See MDN docs for push and apply.

This assumes that you've stored the data from your question in a variable data. If you only have it as a JSON string, you'll need to convert it with JSON.parse.

like image 53
lonesomeday Avatar answered Sep 29 '22 10:09

lonesomeday