5

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

2 Answers 2

10

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.

2
  • Would it be possible to create a multi-dimensional array with rows and then the individual data in each row? Commented Nov 10, 2011 at 21:43
  • It's already a multidimensional array, just use data.table.rows to grab it Commented Nov 11, 2011 at 14:53
2

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.