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.

How to print one of the attribute value from JSON payload to response body?

Not applicable

Scenario: I need to print one of the attribute value from JSON payload from 'Query parameter' to response body.

Example: In the payload it is given:

"site": {
"name":"abc demo"
}

So in this case i need to print the name value that is 'abc demo' in response body.

Issue: I wrote code but it is giving below error:
"message": "Cannot read property \"name\" from undefined",

Script:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {


var requestbodydata = request.queryParams.input[0];
var test = requestbodydata .site.name;

return(test);

}

 

Any idea how to solve this?

1 ACCEPTED SOLUTION

Do one thing:

var requestbodydata = JSON.parse(request.queryParams.input[0]);
return requestbodydata.site.name;

 

Best Regards
Aman Kumar

View solution in original post

7 REPLIES 7

Aman Kumar S
Kilo Patron

Hey,

You have a spcae:

var test = requestbodydata .site.name; remove this, it should work

Best Regards
Aman Kumar

Hi Aman.

 

In the actual script there is no space.

 

Regards,

Vishal

Do one thing:

var requestbodydata = JSON.parse(request.queryParams.input[0]);
return requestbodydata.site.name;

 

Best Regards
Aman Kumar

getting error:

 

"error": {
    "message": "Expected end of stream at char 6",