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-28-2016 06:47 AM
Hi Ashwani,
I think you need to use "decode" instead of encode.
"json.data.element1" should work, you can also try json['data']['element1'].
try logging the values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2016 07:06 AM
Thanks for your response, I did tried using decode which gave the result as object,Object
Even tried dot walking hoping this is an object which will provide some dot walking results, but it gives undefined for the first case and script evaluation exception for second one.
Even if i simply log (request.body.data) , i can see [object Object].
So tried encoding instead, which provides the request body atleast..
Any other code which you tried for the same?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2016 03:58 PM
Use "request.body.data.toString()" to get the string value for logging.
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2016 07:16 AM
Hi Ashwani,
encode function is used to convert JSON object to String format. So, Input would be JSON Object and output would String.
var text = json.encode(obj);
Where as, decode function is used to convert JSON String to SON Object. So, Input would be String and output would be JSON Object.
var obj = json.decode(text);
As Sumeet suggested,decode seems to be more suitable function in your case.
Cheers
Srini