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.

The payload is not valid JSON

Royal1
Tera Contributor

r.setStringParameterNoEscape('comments', current.comments.getJournalEntry(1));

 

while sending the comments inthe above REST API form , i am geeting the below error 

 

Please let me know how to send the work notes or comment  in REST calls

error:

 

responsebody {"error":{"message":"Exception while reading request","detail":"The payload is not valid JSON."},"status":"failure"}

1 ACCEPTED SOLUTION

@Royal1 

use this and do this for all the values

function jsonEncode(str) {
	str = str.replace(/\n/g, "\\n").replace(/\r/g, "\\r");
	return str;
}

var comments = current.comments.getJournalEntry(1);

// use this to remove the timestamp and user details and just get the value/text part
var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;
comments = comments.replace(dateRE, '');	

r.setStringParameterNoEscape('comments', jsonEncode(comments));

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

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

View solution in original post

6 REPLIES 6

@Royal1 

use this and do this for all the values

function jsonEncode(str) {
	str = str.replace(/\n/g, "\\n").replace(/\r/g, "\\r");
	return str;
}

var comments = current.comments.getJournalEntry(1);

// use this to remove the timestamp and user details and just get the value/text part
var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;
comments = comments.replace(dateRE, '');	

r.setStringParameterNoEscape('comments', jsonEncode(comments));

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

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

Thanks you Ankur