Skip to Content

Issues parsing GET response in XSJS

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;
});
Add a comment
10|10000 characters needed characters exceeded

  • Oh duh. I was using logic I've previously successfully implemented for another set of AD groups, but didn't even think to look at that. I've updated the code to use the correct values from the result set, however, still erroring. I think part of the issue still is that the response is still not being interpreted as JSON even though I'm trying to explicitly parse it as such. I always return [object Object] or undefined.

            var resultArr = [ [''] ];
            var innerArr = [];
            jbody.Results.forEach(function(element, index){ // Results is first key to access
                innerArr = [null ,null];
                attributeArr.forEach(function(attr, i){
                    if (element.GroupUsers.hasOwnProperty(attr)){ // Group Users is next key to access
                        innerArr[i] = element.attributes[attr];
                    }
                });
                resultArr[index] = innerArr;
            });
    

Assigned Tags

Related questions

1 Answer

  • Posted on 2 days ago

    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
    10|10000 characters needed characters exceeded

    • I also tried your logic, but receive the same error: jbody.Results is undefined. I want to be able to pull a list from AD whenever is neccessary with the updated list of users. The response body says its application/json but I'm starting to think otherwise.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.