I cannot figure out how to loop though this JSON return properly. It seems formatted properly but using JSON.Parse() just isnt working for me. It does work for the first two $POST variables that are returned and I can use them without issue. Im wondering if im having so many issues because I'm getting this JSON return from the echo'd JSON array that im echoing out back to the ajax. Whats strange to me is that ive saved this to a local.json file and I can parse it without issue.
{
"0": {
"Name": "QG",
"Guid": "3e2e4830-7954-4248-b580-03aa04f3add8",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"1": {
"Name": "OR",
"Guid": "51f61b92-2b88-4820-a3a4-042c79844e5d",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"2": {
"Name": "4O",
"Guid": "90ccb0c9-ab7a-4d3e-94d0-09a9983bbce7",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"3": {
"Name": "VV",
"Guid": "16047f72-2ee0-4cc0-acd8-0c19e9a2694c",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"4": {
"Name": "5F",
"Guid": "4e7bcd95-44fd-4d10-a586-14a5e77f6e22",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"5": {
"Name": "FR",
"Guid": "1ca5a5ce-ac5c-4fd7-8f10-14a8585d4de9",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"6": {
"Name": "6B",
"Guid": "f76307dd-4500-4ace-a756-1b4b207801c2",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"7": {
"Name": "AK",
"Guid": "3ffbb7a5-e5a4-458f-a9c7-1f8a29df58dc",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"8": {
"Name": "MK",
"Guid": "e51e3daa-d10f-43ce-b3ce-202bb9e5f227",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"9": {
"Name": "VH",
"Guid": "c332aa7b-0a3a-4b24-920a-20e4273c7902",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
},
"10": {
"Name": "3K",
"Guid": "d33da5b4-29cf-401f-8880-27d773d56d2b",
"TypeName": "PresentationFolder",
"LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
"TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
"ParentFolderName": "HELLO_WORLD",
"ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
"Attributes": "Normal",
"Enabled": "false",
"IsSchedulableItem": "false",
"ScheduleEnabled": "false",
"SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
}
UPDATE:
I was finally able to to get better results and get the JSON displaying correctly. By setting my variable to var json = data.body[0]; and outputting like this $('.modal-body').html('<pre>' + JSON.stringify(json, null, 2) + '</pre>');
I just cant figure out this .each() statement now to iterate though all of the objects I only get the last value at the bottom of this JSON.
FINAL CODE IT WORKS :D
if (data.body[0][1] !== undefined) {
var json = data.body[0];
} else {
var json = data.body;
}
const mappedItems = $.map(json, function(item) {
const html = `<div class='table-responsive' style='padding-top:20px;'>
<table class="table color-bordered-table info-bordered-table">
<thead>
<tr style="white-space: nowrap; text-align: center;">
<th>IATA</th>
<th>Guid</th>
<th>Type Name</th>
<th>Long Type Name</th>
<th>Type Guid</th>
<th>Parent Folder Name</th>
<th>Parent Folder Guid</th>
<th>Attributes</th>
<th>Enabled</th>
<th>Is Scheduled Item</th>
<th>Schedule Enabled</th>
<th>Drill Down</th>
</tr>
</thead>
<tbody>
<tr>
<td>${item.Name}</td>
<td>${item.Guid}</td>
<td>${item.TypeName}</td>
<td style="inline-size: 160px;word-break: break-all;">${item.LongTypeName}</td>
<td>${item.TypeGuid}</td>
<td>${item.ParentFolderName}</td>
<td>${item.ParentFolderGuid}</td>
<td>${item.Attributes}</td>
<td>${item.Enabled}</td>
<td>${item.IsSchedulableItem}</td>
<td>${item.ScheduleEnabled}</td>
<td><button type="button" name="'.$value->Name.'" value="'.$value->Guid.'" class="btn btn-primary"
style="width:150px" onclick="modalData(this)">Full screen</button></td>
</tr>
</tbody>
</table>
</div>`;
return `${html}`;
});
$('.modal-body').html(mappedItems);
value? Second, for each iteration of the loop, you are replacing the child HTML of.modal-body. Therefore, at the completion of your loop, the HTML of.modal-bodywill contain the data of the last element in your parsed JSON object.