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

Ankur Bawiskar
Tera Patron
Tera Patron

@Royal1 

when you use this current.comments.getJournalEntry(1) it gives you timestamp and user detail as well

try this once

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', 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

Royal1
Tera Contributor

its removing the time stamp , but my comments are not going to another system. still the same error, check the screenshot   

 

Royal1_0-1677571638425.png

 

@Royal1 

can you try to hard-code the comments ?

if it still doesn't work check with the 3rd party team and inform them about the same

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

Royal1
Tera Contributor

i found the issue but dont know the solution , same issue for description fields as well.

 

 

if i am sending the data into second line or multiple lines i am getting this error. with single line data no issues.

 

 

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

with 400 http response code

below is the body:

Royal1_0-1677575796320.png