I'm using an xsjs service to make a call to an AD group via an API. The API call is successful and I am able to get the information that I need.
However, when I get the response and try to parse through to get the required data (username, name) I'm getting 'undefined' errors. I've wracked my head, and can't find a solution.
I've designated the content type to come back as application/json, and I've tried inputting the response body into an array first, JSON.parse(body), and not converting the body, but using it as it comes in. No matter what I try the result is undefined.
I am able to print the body asString to the console and see the correct information. However, anytime I go to do anything with the json object itself I cannot.
Example output in the console of the JSON.parsed object: [object Object]
Example output from the server as a string, scrubbed:
{"GroupMembersLookupRequested":{"GroupId":"9999999"},"IsError":false,"Errors":[],"ResultCount":1,"Results":{"Groupresult":{"GroupId":"9999999","GroupName":"ITExampleGroup","GroupSysCode":"IT","GroupUsers":[{"StaffId":"012345678","Cn":"123456","DisplayName":"Doe, Jane A"}],"NonUserMembers":[]}}}
$.response.contentType = "application/json"; var reqString = "/9999999"; var attributeArr = ["Cn","DisplayName"]; var dest = $.net.http.readDestintation("SERVICE","AD"); var client = new $.net.http.Client(); var req = new $.web.WebRequest($.net.http.GET, reqString); client.request(req, dest); var response = client.getResponse(); if(response.body){ var body = response.body.asString(); var jbody = JSON.parse(body); var resultArr = [ [''] ]; var innerArr = []; jbody.resources.forEach(function(element, index){ innerArr = [null ,null]; attributeArr.forEach(function(attr, i){ if (element.attributes.hasOwnProperty(attr)){ innerArr[i] = element.attributes[attr]; } }); resultArr[index] = innerArr; });
Where does the "resources" array property in jbody comes from? In the string output there is no such property available, so the JSON.parse will not create such a property too. Also the usage of element.attributes is not valid, because your result does not have such a property too.
Is it necessary to have such a dynamic logic you try to implement or has the response body a model/structure on which you can refer?
In case of a fix structure you can do a simpler approach. One example (with old XSJS compatible syntax):
var jbody = { "GroupMembersLookupRequested": { "GroupId": "9999999" }, "IsError": false, "Errors": [], "ResultCount": 1, "Results": { "Groupresult": { "GroupId": "9999999", "GroupName": "ITExampleGroup", "GroupSysCode": "IT", "GroupUsers": [ { "StaffId": "012345678", "Cn": "123456", "DisplayName": "Doe, Jane A" }, { "StaffId": "0815", "Cn": "0815", "DisplayName": "BlaBla" } ], "NonUserMembers": [] } } }; var searchAttr = ["Cn", "DisplayName"]; var result = []; jbody.Results.Groupresult.GroupUsers.forEach(function(groupUser) { var resEntry = {}; searchAttr.forEach(function(attr) { if(groupUser[attr]) { resEntry[attr] = groupUser[attr]; } }); if(Object.keys(resEntry).length > 0) { result.push(resEntry); } }); console.log(result);
Add a comment