1

I have a JSON file that I would like to create an multi-dimensional array from. This information is stored in a JSON file.

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 of arrays from the objects in the "rows" section.

Any help would be appreciated!

Thanks!!!

3 Answers 3

1

Once you parse the JSON, the "table.rows" property will already be a multi-dimensional array (2 dimensions, to be specific). All you have to do is access it:

var array2D = parsed.table.rows;

As to parsing, you should probably use something like Crockford's parser:

var parsed = JSON.parse(rawStringOfJSON);
Sign up to request clarification or add additional context in comments.

2 Comments

How would I get the rawStringofJSON from a .json file?
Well, that depends on your server application. Generally you'd fetch it with some sort of AJAX interaction, but it could be included in a page in other ways. That probably should be a separate question, as it really doesn't have much to do with the parsing aspect of JSON.
0

You first need to convert the string to JS object.

Use json2.js from http://www.json.org/js.html

Please keep in mind that you need to add json2.js if you consider to support old browsers. Newer browser has built-in support for JSON

And deserialize the string to object

var myObject = JSON.parse(myJSONtext);

And now you can access

myObject.table.rows[1][2]; // yields data12

Comments

0

The rows section already contains an array of arrays, so just use:

var result = JSON.parse('{
  "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"]
   ]
  }
}');

var rows = result.table.rows;

Comments

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.