Viewing Values in JSON

I've been struggling with handling JSON values for some time now. When making an AJAX call, I receive JSON values and now I need to display them in a specific format. For instance, let's say the returned values are

{"Heading 1":["Content 1","Content 2"],"Heading 2":["Content 1","COntent 2","Content 3"],"Heading3":["Content 1"]}

I would like to present them as follows:

<h4>Heading 1</h4> <input type="checkbox">Content 1<br><input type="checkbox">Content 2<br>
<h4>Heading 2</h4> <input type="checkbox">Content 1<br><input type="checkbox">Content 2<br><input type="checkbox">Content 3<br>
<h4>Heading 3</h4> <input type="checkbox">Content 1<br>

Any suggestions on how to achieve this?

Answer №1

Give this method a try:

let information, boxHolder, keyword, currentHeader, headerElement, index, check;

information = {"Section A":["Data 1","Data 2"],"Section B":["Data 1","Data 2","Data 3"],"Section C":["Data 1"]};

boxHolder = document.createElement("div");
for (keyword in information) {
    currentHeader = information[keyword];
    headerElement = document.createElement("h4");
    headerElement.innerHTML = keyword;
    boxHolder.appendChild(headerElement);
    for (index = 0; index < currentHeader.length; index++) {
        check = document.createElement("input");
        check.type = "checkbox";
        boxHolder.appendChild(check);
        boxHolder.appendChild(document.createTextNode(currentHeader[index]));
        boxHolder.appendChild(document.createElement("br"));
    }
}

document.body.appendChild(boxHolder);

DEMO: http://example.com

This code can be executed within the window.onload event (similar to the jsFiddle example)

Answer №2

let data ={"Title 1":["Text 1","Text 2"],"Title 2":["Text 1","Text 2","Text 3"],"Title 3":["Text 1"]}
object = JSON.parse(data);


for ( checkCondition){
    generateElements();

}
addContent();

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

Unable to deploy a node.js package via a jenkins job

I'm looking to set up a Jenkins job that will publish a package to npmjs.com. The source code for the package is located in a GitHub repository. While I am able to successfully publish the package from my personal computer by running 'npm publis ...

Turn off dropdown menu animation (JavaScript)

I am trying to completely disable the transition effect on my drop-down menu. Below is the JavaScript code that I believe needs to be changed: function(e) { if ((/input|textarea/i.test(e.target.tagName) ? !(32 === e.which || 27 !== e.which && (40 !== e ...

Error: Unable to save the image, encountered a TypeError while trying to create a new document using mongoose and express

I recently created a new Product and tried to search for it using the mongoose findById method. However, I encountered an issue while trying to create another document called "Image," which is referenced within the Product schema. The code resulted in a Ty ...

Develop an Azure function using Node.js and pg-promise to insert data into a Postgres database with timescaleDB support

Once again, Azure is causing me frustration. My goal is to take data from an Event Hub and write it to a database. So far, I have successfully received the data at the Event Hub and managed to post it to the database using an Azure function. I would prefer ...

I am struggling to retrieve the data from the Giphy API after making the initial AJAX request

I'm currently in the process of building a basic website that fetches random gifs from the Giphy API. This project is purely for practice, so I'm keeping the site very minimalistic. However, I've hit a snag when it comes to extracting data u ...

Sharing component controllers in Angular2 allows for better organization and reuse of

My dilemma involves two separate page components, Services and Users, that share almost identical methods except for the type classes they use. The templates for both components are similar, but they display different class properties. It feels redundant t ...

directive does not execute when the <Enter> key is pressed

I recently came across a helpful post on Stack Overflow about creating an Enter keypress directive. After following the instructions, here is my JavaScript code that replicates the functionality: JavaScript var app = angular.module('myApp', [] ...

Removing data from firestore/storage does not follow the expected pathway

I have created an image gallery for users using Firebase Storage and React, allowing them to upload and delete images. However, I am facing an issue where the deletion process is taking longer than expected. Expected flow: User clicks on the trashcan ico ...

Selenium fails to detect elements that are easily located by the browser using the aba elements, as well as through the use of JavaScript in the console

Looking to automate actions on the bet365 casino platform, but facing challenges with bot blocking mechanisms. Visit: Struggling to interact with elements within the "app-container" div using Selenium, however able to access them via JavaScript in the br ...

Sending data from Node.JS to an HTML document

Currently, I am working on parsing an array fetched from an API using Node.js. My goal is to pass this array as a parameter to an HTML file in order to plot some points on a map based on the API data. Despite searching through various answers, none of them ...

Assigning a distinct identifier to every item in a dropdown list in MVC

I'm struggling to assign a unique ID to each element in a SelectList (using @Html.DropDownListFor). The current structure of the div in the view is as follows: <div id="Courses"> @Html.DropDownListFor(c => c.Courses, new SelectList(Model ...

JQuery is facing issues with its .each loop when trying to iterate through elements that have dynamically changing class names

When attempting to use variables to search for elements with the same class name as the variable's value within the .each loop below, no alert is triggered. Why is this happening? var NameString = $("#StoreInputs").val(); var NameArray = NameString.s ...

Trouble integrating JSON data with Newtonsoft.Json library in WPF applications

Currently, I am working on extracting data from the office365 API that returns responses in JSON format. My aim is to store specific data such as Id and DisplayName into variables for further use, but I seem to be struggling with the process. I came across ...

What methods can be used to access web content with Java programming language?

Reading this foreign language content online has been quite challenging for me. Currently, I am working on an android app that requires reading JSON data from the web. Despite my attempts to figure it out on my own by Google searching, nothing seems to be ...

React Router Error: Hook call invalid. Remember to only use hooks inside the body of a function component

I am having an issue with my React app that uses react router. In Box.js, I am attempting to access the URL parameters but I am encountering the following error message: Invalid hook call. Hooks can only be called inside of the body of a function component ...

How can I ensure that the display only updates upon clicking the submit button, and not every time the search is modified

The current functionality of the search bar displays results as the user types, but I want it to only show results after clicking the submit button. How can I achieve this? function Schedule(props){ //array hook for calendar data const [calendar ...

Having trouble uploading a JSON array file to your Elasticsearch index?

I've been attempting to upload a json array file into elasticsearch using the commands below: curl -XPOST 'http://localhost:9200/unified/post/1' -d @unified.json as well as curl -XPOST 'http://localhost:9200/unified/post/_bulk' ...

Utilize React.js to showcase a component featuring an onClick event handler

Just starting out with React, I'm working on a basic React application that showcases all the countries in the world on the screen along with a small search bar to display data for the searched country. Click here to see a screenshot of the site I&a ...

Issues with Navigation and Routing in Ionic and AngularJs are causing trouble

I have recently started learning AngularJs and Ionic, and I am attempting to replicate the functionality from this tutorial to create a simple app. However, all I see is a blank screen with no errors in the console log. index.html <!DOCTYPE html> & ...

Ajax is displaying some unusual behavior

Currently, I am working on an ajax request to check if a specific combination of username or password exists. <script> $("form").submit(function(e){ e.preventDefault(); //send data to ajax file now $.ajax({ type: 'POST ...