Create Nested JSON dynamic Payload for consuming external REST API

sachin_namjoshi
Kilo Patron
Kilo Patron

Hello All,

I am building bidirectional integration of service now to zendesk  using REST API.I created REST message for consuming REST API, setup token based authentication and rest of configuration is completed.

I need to consume zendesk REST API in servicenow.

Issue is zendesk expects nested JSON request body like below.

 

{
  "ticket": {
    "comment": { "body": "sachin chofosing dddAcme Jet Motors.", "public": true },
    "status":  "solved"
  }
}

 

In above example,  comment and status will be dynamic from incident record.

Comment body from above example is incident record comments and status will be incident state values.

How do i build this nested JSON request body to consume external REST API? Can someone give me sample code to build this kind of nested JSON payload?

I am using below REST API methods to consume external REST API.

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/sn_ws-namespace/c_RESTMessage...

 

Regards,

Sachin

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sachin.namjoshi 

you can form the json object in your script and then use setRequestBody() to set the json string in the body of POST/PUT method

var finalObj = {};

var commentObj = {};
commentObj['body'] = 'sachin chofosing dddAcme Jet Motors.';
commentObj['public'] = 'true';

var obj = {};
obj['comment'] = commentObj;
obj['status'] = 'solved';
finalObj['ticket'] = obj;

gs.info(JSON.stringify(finalObj)); // this would give you your nested json

var request = new sn_ws.RESTMessageV2();

request.setRequestBody(JSON.stringify(finalObj));

If my answer solved your issue, please mark my answer as āœ… Correct & šŸ‘Helpful based on the Impact.

Regards
Ankur

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

View solution in original post

4 REPLIES 4

premer
Giga Expert

Hello,

here is some sample code we use to send a nested json body in an api call:

 

var body = {
			"subject":"PC Replacement",
			"start":{
				"dateTime":start_dt,
				"timeZone":"UTC"
			},
			"end":{
				"dateTime":end_dt,
				"timeZone":"UTC"
			},
			"location":{
				"displayName":location.toString()
			},
			"body":
				{
					"contentType":"HTML",
					"content":getInviteContent()
				},
			"attendees":[
				{
					"emailAddress":{
						"address":email.toString(),
						"name":name.toString()
					},
					"type":"required"
				}
			],
			"categories":[				
					"Orange Category"
				],
		};

To send it in the api call,we then use

sendInviteRest.setRequestBody(JSON.stringify(body));

in your RESTMessageV2 definition.

Ankur Bawiskar
Tera Patron
Tera Patron

@sachin.namjoshi 

you can form the json object in your script and then use setRequestBody() to set the json string in the body of POST/PUT method

var finalObj = {};

var commentObj = {};
commentObj['body'] = 'sachin chofosing dddAcme Jet Motors.';
commentObj['public'] = 'true';

var obj = {};
obj['comment'] = commentObj;
obj['status'] = 'solved';
finalObj['ticket'] = obj;

gs.info(JSON.stringify(finalObj)); // this would give you your nested json

var request = new sn_ws.RESTMessageV2();

request.setRequestBody(JSON.stringify(finalObj));

If my answer solved your issue, please mark my answer as āœ… Correct & šŸ‘Helpful based on the Impact.

Regards
Ankur

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

Hello Ankur / Team,

Greetings for the day!. I am also trying to integrate ServiceNow with Zendesk and want to update comments from Servicenow to Zendesk ticket using PUT method.In the above solution I am trying to pass current record's work_notes/ additional comments dynamically to the request body. 

this is what I triedcommentObj.body = current.work_notes; (tried with comments_and_work_notes column/ other string fields too) but the line only accepts string that present in between"", in that case how to pass dynamic field value in the request body.

 Could you please guide here! 

 

Thanks in advance.

 

Regards,

 

Kalai

Hi,

So you are consuming Zendesk API and want to send comment/work notes of current record.

I didn't get how to pass dynamic field value in request body.

you are already fetching the value using current object

Regards
Ankur

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