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

Thanks Aman,

working now

Awesome!

Best Regards
Aman Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

what type of HTTP method it is?

Can you share the complete query parameters?

share what comes here

var requestbodydata = request.queryParams;
gs.info("JSON " + JSON.stringify(requestbodydata));

regards
Ankur

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