Transforming JSON into a Multi-Dimensional Array using JavaScript

Looking for assistance in converting JSON data into a multi-dimensional array. The information is currently stored in a JSON file.

Below is the JSON data structure:

{
  "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"]
     ]
   }
}

In order to achieve this, an array of arrays needs to be created from the objects within the "rows" section.

If anyone could provide guidance on this task, it would be greatly appreciated!

Thank you in advance!!!

Answer №1

After successfully decoding the JSON data, you will find that the "table.rows" attribute is already structured as a two-dimensional array. To retrieve it, simply access it like this:

var tableArray = decodedData.table.rows;

For parsing the JSON, it is recommended to utilize a reliable parser such as Crockford's JSON parser:

var decodedData = JSON.parse(jsonString);

Answer №2

Start by converting the string into a JavaScript object.

Download and use json2.js from http://www.json.org/js.html

Remember to include json2.js if you want to support older browsers. Newer browsers have native JSON support.

Next, deserialize the string into an object

var myObject = JSON.parse(myJSONtext);

You can now access the data

myObject.table.rows[1][2]; // will give you data12

Answer №3

To access the rows in the existing array of arrays, you can simply do the following:

let data = JSON.parse('{
  "table": {
    "columnNames": ["A", "B", "C", "D"],
    "columnTypes": ["String", "String", "String", "String"],
    "rows": [
      ["row1-data1", "row1-data2", "row1-data3", "row1-data4"],
      ["row2-data1", "row2-data2", "row2-data3", "row2-data4"],
      ["row3-data1", "row3-data2", "row3-data3", "row3-data4"],
      ["row4-data1", "row4-data2", "row4-data3", "row4-data4"]
    ]
  }
}');

let rows = data.table.rows;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating deprecated CanActivate in Angular-15 with Auth Guard

Authentication Guard: import { Injectable, inject } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, CanActivateFn, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs& ...

php map values of an array to keys of another array

I am working with two arrays $firstArray = Array ( [0] => 12 [1] => 4 [2] => 8 [3] => xx [4] => 1 [5] => 1year [6] => 7 ) $secondArray = Array ( [0] => 1 [1] => 2 [2] => 3 [3] => ...

Tips on sorting ID strings that include a specific substring

I have a user input that may include a street, postal code, city, or a combination of them. How can I filter an array of objects to find those that contain any of these strings in the corresponding fields? getFilteredCentersSuggestions(searchTerm: string) ...

How can I retrieve nested JSON data efficiently from a database?

Consider the following scenario with two tables: PRODUCTS (id, category_id, name); CATEGORIES (id, name); The goal is to provide JSON data to the frontend in the following format: "categoryProjects": [ { "id" : 1, "name" : "some cate ...

How can I filter an array by a nested property using Angular?

I need help with objects that have the following format: id: 1, name: MyObj properties: { owners: [ { name:owner1, location: loc1 }, { name:owner2, location: loc1 } ] } Each object can have a different number of owners. I' ...

parsing an array using jQuery's getJSON

Finally got the simplified array to work, check it out below If you're still struggling with parsing a complicated array, you can refer to this link. TLDR: I need to extract and insert each heading from an array into a div using Jquery - getJSON, wi ...

Encountering the error "Error: Maximum update depth exceeded" while coding a React private Route with infinite

Attempting to render components inside private routes only if the user is authenticated, but encountering an error message that reads: "Error: Maximum update depth exceeded." This issue typically arises when a component continuously calls setState within c ...

The checkbox is not updating as anticipated

I'm currently developing a basic widget that allows the user to change the value of either the check box OR the entire div by selecting them. Below is the code I have implemented: $(document).ready(function() { $(document).on("click", ".inputChec ...

Attempting to deliver a static JSON file to an API endpoint using Node and Express

I have been developing a MEAN stack application and I am currently working on setting up API endpoints that can be accessed by the Angular client through the $http.get method. These endpoints will serve simple JSON files filled with placeholder data for te ...

Error: Unable to locate module: Unable to locate '@material-ui/core/Container'

Encountering an error in the browser: Error message: Module not found: Can't resolve '@material-ui/core/Container' The search for the component is directed towards my components directory instead of node_modules. Unfortunately, changing ...

Utilize Vuex mutators within route navigation guards

Hey there, I'm currently working on an app using Laravel and VueJS. To restrict certain routes, I've implemented navigation guards. However, I'm facing an issue where I need to access Vuex mutators to determine if the current user is logged ...

Node.js allows for downloading files from an FTP server to a client easily

System architecture The system is composed of 3 main components: 1: FTP server: Exclusively used for file storage, accessible only through the Node.js app. No direct access permitted. 2: Node.js: Acts as an API to interact with the FTP server. It is the ...

JSON parser made easy

Greetings! I am currently facing an issue with a JSON simple parser. I need to parse data that looks like this {"phoneNumber":"9874662222","name":"Иван","surname":"Иванов","key":"ivanov0"}{"phoneNumber":"9874662222","name":"Иван","surname":" ...

Manipulating a JSON object within a JSON array on an Android device

My goal was to store objects in an array and then display them in a spinner named "caracteristici". However, the project faced a roadblock when it came time to put the objects in the array. try { JSONObject jObj = new JSONObject( ...

Refresh in AJAX, automated loading for seamless transition to a different page

Having an issue with the page not auto-refreshing, although it loads when I manually refresh. P.S Loading the page onto another page. Below is my HTML and AJAX code along with its database: The Trigger Button <?php $data = mysqli_query ...

Ways to emphasize an HTML element when clicked with the mouse

Can JavaScript be used to create code that highlights the borders of the HTML element where the mouse pointer is placed or clicked, similar to Firebug, but without needing a browser extension and solely relying on JavaScript? If this is possible, how can ...

What is the best way to link the roll button to a specific video URL?

I am currently working on a project that involves assigning specific videos to 6 roll buttons for continuous play. For instance, I want the first roll button to display a yellow dice and the second button to show a blue dice, and so on. As of now, I have ...

What are the steps for implementing videojs vr in a Vue.js project?

After installing the videojs vr with npm install --save videojs-vr, I attempted to use it in my project: https://i.stack.imgur.com/lSOqF.png However, I encountered this error message: https://i.stack.imgur.com/QSe1g.png Any assistance would be greatly ...

"Enhancing user experience with advanced checkbox filtering in Django using JavaScript

For filtering my results on an HTML page, I am attempting to use checkboxes. I pass my data in a list through a Django view, and then iterate through it using a loop like this: </div> {% for r in rows %} <div class="res_show"> ...

How can I ensure a successful redirect to react-router root path after saving to MongoDB in Express?

As a newcomer to React and react-router, I may be making some rookie mistakes in my project. Currently, I am constructing a web application with React and react-router as the frontend server, paired with Express and MongoDB for the backend. To communicate ...