Adding JSON data to a table with the keys in the first row is a simple process that can be achieved

Previously, I have created tables with XML formatted results and JSON data in the "key: data" format. To access the data, I would use syntax like results.heading1 and then map the data into a table by matching the key with the data.

Now, a new client is only able to send their data in a different format where the keys are all listed in the first row rather than alongside each entry. Despite researching extensively online, I haven't found a solution that fits my specific situation!

How can I create a table when the keys (headings) are not positioned next to the data, but instead appear in the initial row of results?

The JSON data at hand looks like this. When I make an API call to the client, the response mirrors the structure shown below:

resultReturn: {
  success: true, 
  results: [
      "ID, Name, Age",
      "A1, Bob, 31",
      "B1, John, 24",
      "C2, Doe, 160",
  ],
  resultsHTML: ""
}

Answer №1

If the results value is in an array with comma-delimited objects, you can follow this approach:

let resultData = {
  success: true, 
  results: [
      "heading1, head2, head3",
      "string, 200, 1.1",
      "string2, 100, 1.2",
    ]
}

function displayData() {
  let data = resultData.results;
  let tableBody = document.getElementById("datatable");

  for (let i = 0; i < 3; i++) {
    let row = data[i];
    let newRow = "<tr>";
    let rowItems = row.split(",");
    for (let j = 0; j < rowItems.length; j++) {
      newRow += "<td>" + rowItems[j] + "</td>";
    }
    newRow += "</tr>";
    tableBody.innerHTML += newRow;
  }
}
displayData();

Please note that I have enclosed this code block within a function to facilitate testing, so you do not need to add the window.onload line.

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

Custom container width causes animation to crash when scrolling

I'm having trouble with my header. When the containers change with scrolling, an animation takes place. Everything works fine with native Bootstrap CSS, but when I customize the width of the container in my custom CSS (the width set to 1140px), the an ...

What is the most creative way you can think of to create a CSS/JS animated

Is using an animated SVG the best way to create these wavy blobs for mobile compatibility? What approach would you take? Here is a similar example I found var wave = document.createElement("div"); wave.className += " wave"; docFrag.appendChil ...

What are some strategies for implementing dynamic script generation within an AJAX response?

I am exploring a new AJAX design approach where a script is returned to handle and show data. The JSON response below is currently functional, but I would appreciate advice on how to better organize the application for future maintenance. { payload: " ...

"Exploring the differences between sending a JSON post request with multiple parameters to a Web

Our transition from MVC to WebApi for data serving involves changing the controller structure, as shown in the example below. MVC: OfficeController : Controller { [HttpPost] public IEnumerable<Employee> SetSalary(string badge, string salary ...

Unable to receive data from jQuery AJAX request

I'm feeling a little puzzled at the moment. Whenever I run my ajax call, the error function is triggered every time. I am aware that the data is returning as JSON, and I have set the datatype as jsonp to enable cross-origin functionality. I am not sur ...

Remove any blank way point entries from the JSON data

Is there a way to modify this code so that if the corresponding entry box is left blank, no information on a "way point" will be included in the data? In other words, if nothing is entered for E2, I want the data to be [ { 'Way point1':(2,E1.get( ...

Tips for updating a URL using data obtained from a JSON response

As I loop through an array from JSON, I extract all the necessary information. For example, if I have cards for blog posts that include the title, short description, published date, and URL. When clicking on a card, it redirects to a corresponding page ba ...

Discovering the technique to dynamically load various content using jQuery in a div based on its

After discovering this code to extract information from the first div with an id of container (box4) and retrieve any new data from the URL, everything was functioning correctly. However, I encountered an issue when attempting to load box5 and rerun the co ...

The Javafx WebEngine's executescript() function is unable to send a multiline string

Issue: I'm struggling to make the JavaFX WebEngine trigger a JavaScript function when using a Java String object with multiple lines. If I input the following example in HTML: "asd" ENTER "qwe" into the textArea, and then click the send button, the f ...

What are the implications of incorporating listeners in redux action creators?

While developing my app, I have a feature that involves constantly monitoring location changes and updating the store accordingly. One question that has arisen is whether it would be beneficial to keep the listeners inside the action creator rather than th ...

Creating a webpage with a feature that allows users to click a button and instantly change the background to a

Here is the code I have for generating random colors: const colors = ["green", "red", "rgba(133,122,200)", "#f15025"]; const btn = document.getElementById('btn'); const color = document.querySelector('.color'); btn.addEventListener(&a ...

jQuery form validation not functioning as expected

I'm attempting jQuery form validation but encountering issues with the desired functionality. I would like the border of an input to turn red when it's empty upon focus out. Alternatively, I aim to incorporate the "has-danger" bootstrap class to ...

How to implement a form in PHP that doesn't refresh the page upon submission

I am having an issue with this ajax code. I know it works, but for some reason it's not submitting. <script type="text/javascript"> $(function(){ $('input[type=submit]').click(function(){ $.ajax({ type: "POST", ...

How to check Internet upload speed in Angular without using a backend server?

I need help uploading a file to a folder within my Angular app's directory while it is running on localhost. I have been unable to find a solution that doesn't involve using backend technologies. For instance, I simply want to upload an image fi ...

Access an attribute using slashes in Jquery

I've been struggling to use jQuery to select an object based on a unique filename attribute. However, I'm facing issues with escaping slashes when the selector is created using a variable. Despite spending hours trying different combinations, I s ...

Using React and Redux to showcase JSON data on a webpage

I am trying to load local JSON data using Redux and display it in a React app. However, I am encountering the error pageId is undefined in the reducer. I'm not sure where I might be going wrong here. It seems like there may be an issue with how I am ...

How can I use Jackson to wrap properties in Micronaut?

I am in the process of developing a microservice with Micronaut and utilizing Jackson. One challenge I have encountered is how to nest properties within the JSON without creating a separate class. This is what the Entity JPA Class looks like: @Entity @Tab ...

Retrieve HTML content from a JSON object and render it on a web page

I am facing a challenge with decoding html that is in json format. I'm struggling to figure out how to retrieve my html and display it on a page. It seems like json_decode isn't the solution. Can anyone help me with this issue? Appreciate any as ...

Is there a way to bypass cells within an html table?

Creating an online board game and looking to have the cells go around the board. Wondering if anyone knows a way to skip the cells in the middle? Attempted using   but it doesn't seem to be effective. Here is a snippet of the code: #board { ...

Issues encountered with making JSONP requests

Hey there, I'm a bit stuck and need some help figuring out what's going wrong. Sometimes a fresh perspective can make all the difference. So, here's the situation: I'm making a JSONP request using jQuery from one domain () to a PHP scri ...