I am trying to index through a JSON list in JavaScript but I'm having quite a lot of trouble getting it to work.
I have tried indexing through the values of each JSON item but it's not outputting what I want. I think I can get it to work but that would be with an extra unnecessary for loop level. Here is my JSON: https://pastebin.com/iYmaA4c5. If you think it would be better to reformat the JSON then that might help too.
function output_json(data) {
var i, j, k;
for (i=0; i<Object.keys(data).length; i++) {
group_=Object.values(data)[i];
for (j=0; j<Object.keys(group_).length; j++) {
person=Object.values(group_)[j];
person_id=Object.keys(person)[0];
console.log(person_id);
for (k=0; k<Object.keys(person).length; k++) {
person_info=Object.values(person)[k][0];
console.log(person_info);
}
}
}
}
I want it to print out the id, and then the name, registration 1, registration 2, week and half_term for each id. At the moment it prints like this:
HA09_000
{name: "Wyatt Feldt", registration_1: "R", registration_2: "R", week: 0, half_term: 1}
But I want it to print like this for every single one:
HA09_000
Wyatt Feldt
R
R
0
1
Here is some Pseudo Code for what I would like to achieve:
FOR GROUP IN DATA:
FOR PERSON IN GROUP:
PRINT(PERSON.ID)
FOR INFO IN PERSON:
PRINT(INFO)
Thanks.
EDIT: This is the function I am using to retrieve the data:
$.getJSON("http://localhost:8000/data.json", function(data) {
output_json(data);
});