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

I have tried with below code but it is not working.

 var namearr = name.split(',');

            for (var j = 0; j < namearr.length; j++) {

                gs.print("inside for: " + namearr[i].toString());

                var gr = new GlideRecord("u_jira_projects");
                gr.query();
                gr.initialize();
                gr.u_name = namearr[i].toString();
                gr.insert();
            }

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

It works.

Thank you.

below is my custom table.

find_real_file.png