How to fetch the JSON elements value - any function available OOB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2016 06:37 AM
By using the below code, i am able to get the exact JSON request body which is getting passed from another application
var json = new JSON().encode(request.body.data)
{
"code":200,
"params":{},
"data":{
"element1":" 111",
"element2":"234",
"element3":"567",
"element4":"2015-01-18",
}
}
I need to fetch one of the elements value (element2) from within this JSON request body and further use that for updating a record's field.
How can i fetch this , is there any function available OOB. In JSON Script include, i cannot see any function to accept a key .which returns a value.
I even tried using dot walking (like json.data.element1, also json.element1,) but it gives me undefined error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2016 08:23 PM
Thanks for your responses, i tried them all, but it did not worked for me.
Request sending from Rest API Explorer
I am getting the JSON request in Scritped web services, and trying to fetch the value of one of the element inside that JSON structure.
1) var jsonresp =new JSON().decode(request.body.data.toString()));
gs.log(jsonresp) -- returns object,Object
gs.log(jsonresp.data.element1) -- returns undefined
2) var json = new JSON().decode(request.body.data);
var value = json["data"]
gs.log(value) --- returns undefined
3) var parser = new JSONParser();
var json_alert = parser.parse(request.data.body);
gs.log(json_alert.data.length)---returns undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2016 07:47 PM
Hi Ashwani,
Are you sure you are getting the object in below format only? Cause in that case, we can directly use it as JSON object. Just try logging "json.data.element1" directly, it should give "111" as output without using any JSON parser.
{
"code":200,
"params":{},
"data":{
"element1":" 111",
"element2":"234",
"element3":"567",
"element4":"2015-01-18",
}
}
If that's not the case, can you paste the exact response you are getting from other application?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2016 01:41 PM
Any idea how to do I traverse to "descr" in to below javascript object
I am calling a web service method which returns response as XML, then I feed that response in to XMLHelper and gets a JS object
var xmlhelp = new XMLHelper(response);
var objJson = xmlhelp.toObject();
Below is the output when I print "objJson"
*** Script: Log Object: Our resulting JavaScript object!
Object
S:Body: Object
ns2:getEventsBySelectCriteriaResponse: Object
@xmlns:ns2: string = http://test.com
return: Array of 98 elements
[0]: Object
pageNumber: string = 0
priority: string = 3
eventPath: string = Unknown
eventQueueCount: string = 0
eventUrl: string = https://test.com
modDt: string = 2016-06-23T09:33:54-05:00
protocolId: string = 0
modPgm: string = 30074
severity: string = 2
descr: string = Security Bulletin Alert generated from Bulletin Queue ID 84839. SECUNIA - Security Advisories list test
insertDt: string = 2016-06-27T14:47:13.506-05:00
eventId: string = 141607621
lockedByTypeDescr: string = UNDEFINED
eventDt: string = 2016-06-23T08:59:38-05:00
eventStatusDescr: string = Closed
closeDt: string = 2016-06-23T09:33:53-05:00
srvDescr: null = null
clientContacted: string = 0
lockedFlgDescr: string = UNLOCKED
eventTypeId: string = 15
chgPriorityDt: string = 2016-06-23T08:59:38-05:00
lockedByType: string = 1
sceId: string = 8703
eventJournal: Object
notifyClobString: null = null
eventJournalId: string = 9323237137
pageNumber: string = 0
modById: string = 95881388
assignedUsrId: string = 95881388
createdById: string = 95881388
modDt: string = 2016-06-23T09:33:54-05:00
createdDt: string = 2016-06-23T09:33:54-05:00
modPgm: string = 30074
eventStatusId: string = 3
isControlsJournal: string = false
includeOnRptFlg: string = 1
insertDt: string = 2016-06-27T14:47:13.506-05:00
journal: string = This was a test email.
eventId: string = 141607621
lockedFlg: string = 2
eventStatusId: string = 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2016 08:27 PM
Hi Sumeet,
I am fetching the request from Scripted Rest Web Services , like below
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var json = new JSON().encode(request.body.data);
gs.log(json);
})(request, response);
Output
{
"data":{
"element1":"abc",
"element2":"false",
"element3":[{"node":"s111","Name":"Internet"},{"node":"s222","Name":"Voice"}],
"element4":"Check"},
"params":{}
}
=====
Further,
gs.log(json.data.element1) -- returns undefined
Even tried below code, used decode instead of encode,
var jsonresp = new JSON().decode(request.body.data);
gs.log(jsonresp) -- returns object,Object
gs.log(jsonresp.data.element1) -- returns undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2016 08:44 PM
So i am doing something like this: I take the output which you have stored in "json" and print data.element1.. it prints correctly.
var json = {
"code":200,
"params":{},
"data":{
"element1":" 111",
"element2":"234",
"element3":"567",
"element4":"2015-01-18",
}
};
gs.print(json.data.element1); //111