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

share|improve this question

74% accept rate
feedback

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
Would it be possible to create a multi-dimensional array with rows and then the individual data in each row? – gberg927 Nov 10 '11 at 21:43
It's already a multidimensional array, just use data.table.rows to grab it – Jordan Wallwork Nov 11 '11 at 14:53
feedback

JSON is basically javascript already, so once you've decoded the JSON string back to a native JS data structure, then it'd be a simple matter of doing:

var rowsarray = decoded_json.table.rows;
share|improve this answer
feedback

Use

var abc = (array)json.decode(jsonvariable_name); 

this function will convert the json object in array.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.