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.

unable to read the postman data properly using scripted rest api

Rama26
Tera Contributor

below is the data i have in postman

{

"info": [
{
"minimumvalue": "1",
"desiredValue": "2",
"maximumValue": "3",
},
{
"minimumvalue": "4",
"desiredNodes": "5",
"maximumNodes": "6",
}
]

} for getting these values i written the script in scripted rest api but it's not working

   var details = request.body.data;
var user_min2 = details.info[0].minimumvalue;
var user_min3 = details.info[1].minimumvalue;
When i call the endpoint received below error
{"JobsResponse":[{"status":"failure","statusMessage":"{\"error\":{\"message\":\"Cannot read property \\\"0\\\" from undefined\",\"detail\":\"TypeError: Cannot read property \\\"0\\\" from undefined (sys_ws_operation.dc44b4061b38515022078661cd4bcb36.operation_script; line 56)\"},\"status\":\"failure\"}"
8 REPLIES 8

Hi @Rama26 

var details = request.body;
var jobj= JSON.stringify(details);
var jsonObject = JSON.parse(jobj);
var user_min1 = jsonObject.info[0].minimumvalue;
var user_min2 = jsonObject.info[1].minimumvalue;
gs.info(user_min1);
gs.info(user_min2);
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Rama26
Tera Contributor

or this also received same error

I tried the BG script it's working fine

var jsondata={
    "info": [
        {
            "minimumvalue": "1",
            "desiredValue": "2",
            "maximumValue": "3",
        },
        {
            "minimumvalue": "4",
            "desiredNodes": "5",
            "maximumNodes": "6",
        }
    ]
};


var jobj= JSON.stringify(jsondata);
var jsonObject = JSON.parse(jobj);
var user_min1 = jsonObject.info[0].minimumvalue;
var user_min2 = jsonObject.info[1].minimumvalue;
gs.info(user_min1);
gs.info(user_min2);

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Rama26
Tera Contributor

Same i tried with Scripted rest api it's not working.