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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please update as this and test

you need to parse the responsebody using JSON.parse

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);

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);
}

gs.info("name"+name);
gs.info("id"+id);
gs.info("key"+key);

if (httpStatus == '200') {
    if (responseBody.length != 0) {
        gs.print("responseBody inside if Projects:" );
    }
}

Regards
Ankur

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

Hi @Ankur Bawiskar 

I am getting below output.

find_real_file.png

I got this.

I just wrote 

 var ot = responseBody; 

instead of 

var ot = JSON.parse(responseBody);

 

Thank you Ankur.

Vishwas Kulkarn
Tera Contributor

Hi @Ankur Bawiskar ,

can you please tell me how can I capture below response in custom table?

find_real_file.png

Hi,

for each array iteration you can insert data into your custom table

Regards
Ankur

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