Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Want to fetch field value from response of GET REST message

Vishwas Kulkarn
Tera Contributor

Hi @Ankur Bawiskar 

I am calling GET method and fetching below response.

[{
    "expand": "description,lead,issueTypes,url,projectKeys,permissions,insight",
    "self": "https://.atlassian.net/rest/api/3/project/10013",
    "id": "10013",
    "key": "ACQINT",
    "name": "Acquisition Integrations",
    "avatarUrls": {
        "48x48": "https://.atlassian.net/rest/api/3/universal_avatar/view/type/project/avatar/10412",
        "24x24": "https://.atlassian.net/rest/api/3/universal_avatar/view/type/project/avatar/10412?size=small",
        "16x16": "https://.atlassian.net/rest/api/3/universal_avatar/view/type/project/avatar/10412?size=xsmall",
        "32x32": "https://.atlassian.net/rest/api/3/universal_avatar/view/type/project/avatar/10412?size=medium"
    },
    "projectTypeKey": "software",
    "simplified": false,
    "style": "classic",
    "isPrivate": false,
    "properties": {}
}, {
    "expand": "descrip

 

I want to show id, key and name three fields from above response.

Pls. find my code below

var r = new sn_ws.RESTMessageV2('JiraIntegrationEndpoint', 'Get Issue Details');
        r.setEndpoint("https://.atlassian.net/rest/api/3/project");
        var response = r.execute();
        var responseBody = JSON.parse(response.getBody());
        var httpStatus = response.getStatusCode();
        

        gs.print("responseBody Project:" + responseBody);
        gs.print("httpStatus Project:" + httpStatus);
  
        var ot = JSON.stringify(responseBody);


        gs.print("responseBody Project ID:" + ot);

        var name= [];
        var id = [];
        var key = [];
        for (var i = 0; i < ot.length; i++) {
            name.push('' + ot[i].name);
            id.push('' + ot[i].id);
            key.push('' + ot[i].key);
        }

        if (httpStatus == '200') {
            if (responseBody.length != 0) {

                gs.print("responseBody inside if Projects:" + ot);
}

}

 

Could you please help me in this?

1 ACCEPTED SOLUTION

Hi,

update as this

var r = new sn_ws.RESTMessageV2('JiraIntegrationEndpoint', 'Get Issue Details');
r.setEndpoint("https://.atlassian.net/rest/api/3/project");
var response = r.execute();
var responseBody = JSON.parse(response.getBody());
var httpStatus = response.getStatusCode();

var ot = JSON.parse(responseBody);

for (var i = 0; i < ot.length; i++) {
	var name = ot[i].name;
	var id = ot[i].id;
	var key = ot[i].key;

	var gr = new GlideRecord("u_jira_projects");
	gr.initialize();
	gr.u_name = name;
	gr.u_id = id;
	gr.u_key = key;
	gr.insert();

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

13 REPLIES 13

Maik Skoddow
Tera Patron
Tera Patron

Hi

instead of 

var ot = JSON.stringify(responseBody);

please use

var ot = responseBody;

Maik

I got below output:

find_real_file.png

Hi

this is exactly you can expect if printing the ot object, because it is an object and not text!

Maik

 

I got this.

thank you Maik.