How to extract Object value(value is dynamic in nature) from Scripted Rest API.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2017 12:39 PM
How to extract Object value from Scripted Rest API.example
JSON
{
"catItem":"Epic-Access Request",
"external_company":"SCH",
"external_ID" : "QAZ123456789012",
"variables" : [
{"What is your name":"ABC"}
{"What is FirstName":"A"}
{"What is your Lastname":"BC"}
]
}
Now every time Question (What is your name,What is FirstName,What is LastName) and Answer (ABC,A,BC) will change as per catalog item.
Therefore ,I need to retrieve values of both left side (Question) and right side (answer) in some run time variables to set in Request catalog variables.
I am writing a Scripted REST API as below and stuck as how to write logic for retrieving values of variables Object as it is dynamic in nature.Any response which leads to resolution will be highly appreciated.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var jsonObj = new JSON().decode(request.body.data);
gs.log(jsonObj); //Output object,Object
var catItem = request.body.data.catItem;
gs.log(catItem); ////Output Epic-Access Request
myObjAdd = new JSON().decode(request.body.data.variables);
for (var i in myObjAdd.Question) {
???????????????????
}
})(request, response);
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2017 01:59 PM
Hi Rohit,
var questionSet = myObjAdd.Question;
for(var key in myObjAdd.Question){
if(questionSet.hasOwnProperty(key)) {
console.log(key);// this should give you the question in the questionset
console.log(questionSet["key"]);// this should give you the value
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2017 09:50 PM
Getting this Error
{
"error": {
"message": "\"console\" is not defined.",
"detail": "ReferenceError: \"console\" is not defined. (sys_ws_operation.0d5ed5b237e0034043129b7a93990e17.operation_script; line 29)"
},
"status": "failure"
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2017 05:44 AM
JSON
{
"catItem":"Epic-Access Request",
"external_company":"SCH",
"external_ID" : "QAZ123456789012",
"variables" : [
{"What is your name":"ABC"},
{"What is FirstName":"A"},
{"What is your Lastname":"BC"}
]
}
Below is the CODE with O/p which is not working
CODE with OUTPUT :
var jsonObj = new JSON().decode(request.body.data);
gs.log('Rohit18-08-2017-0: ' + jsonObj); //OutPut : object,Object
var jsonObjStr = new JSON().encode(request.body.data.variables);
gs.log('Rohit18-08-2017-1: ' + jsonObjStr); //OUTPUT [{"What is your name":"ABC"},{"What is FirstName":"A"},{"What is your Lastname":"BC"}]
var questionSet1 = new JSON().decode(request.body.data.variables);
for(var key in questionSet1){
var qazz= questionSet1[key];
gs.log('Rohit18-08-2017-2: ' + qazz);//OUTPUT Rohit18-08-2017-2: function (from, to) {returnthis.replace(from.toString(), to, "g");}
}
//OR
var questionSet2 = new JSON().decode(request.body.data);
gs.log('Rohit18-08-2017-3: ' + questionSet2); //Rohit18-08-2017-3: object,Object
var qwerty = questionSet2 .catItem;
gs.log('Rohit18-08-2017-4: ' + qwerty); //Rohit18-08-2017-4: undefined
var questionSet = questionSet .variables;
gs.log('Rohit18-08-2017-5: ' + questionSet ); //Rohit18-08-2017-5: undefined
for(var key in questionSet){
var qaz= questionSet[key]; //CODE NOT REACHED THIS POINT
gs.log('Rohit18-08-2017-6: ' + qaz);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2017 08:49 AM
Hi Rohit,
Please try this
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var jsonObj = new JSON().decode(request.body.data);
gs.log(jsonObj); //Output object,Object
var catItem = jsonObj.catItem;
gs.log(catItem); ////Output Epic-Access Request
myObjAdd = jsonObj.variables; // this is an array of objects
gs.log(myObjAdd.isArray());// this should return true
myObjAdd.forEach(function(element, index) { // looping through array of variables
if(element) {
for ( property in element ) { // for each element inside the array trying to retrieve key
gs.log( property ); // Outputs "What is your name"
gs.log(element[property]); // Outputs ABC
}
gs.log("Question is " + element.)
}
});
})(request, response);
Now in ECMA Script 5 there is a way to retrieve all keys from an object. That would have been benefecial to you if Variable was an object.
So my suggestion would be to make the incoming JSON better by making the variables an object rather than an array. It would simplify the code far better.
Alternatively, you can make the question an index of the array in variables and even that could be fetched easily.