Read values from json object using keys
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2017 08:45 PM
Hi,
I have a requirement where in i need to parse a json object without knowing the keys i get in the payload.
I found a way to get the keys using new JSON().getKeys(obj); but I could not get the values for specific keys.
I used regular parse techniques but parsing without knowing tags is not possible.
Please let me know how to achieve this
Thanks...
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2017 07:50 AM
Hi Sreedhar,
You can get those values by using payload.u_requested_by and payload.mac_reqtype
javascript - Difference between JSON.stringify and JSON.parse - Stack Overflow

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2017 02:08 PM
Hi Sreedhar,
I think you are trying to do more than you need to... first get your object back from the string payload by parsing:
var obj = new JSON().decode(your_payload_here);
Then you can simply iterate through the members (or keys) of the object as Deepak suggested:
for(var key in obj){
gs.addInfoMessage("Value of " + key + " = " + obj[key]);
}
If you have trouble understanding the process, it can help to check what values you are getting for each little piece of your code so you see whether or not you are getting what you expected. I have a feeling that your two lines:
var l=new JSON().getKeys(obj.variables[0]);
and
for ( var key in l) { ...
are not returning what you expected.
Good luck,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2017 12:36 AM
Hey Sreedhar,
From your incoming data you can try : gs.log(Payload.elementname); // element name is the name of the field you are getting.
post me your feedback
Please Hit ✅Correct, âÂ��Helpful, or ��Like depending on the impact of the response
Have a lovely day ahead
Regards,
Divya Mishra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-14-2018 03:09 PM
Hello Sreedhar,
Please let me know if you could get the values printed. I'm having a similar issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-22-2018 05:38 AM
HI Prathyusha,
I was able to get the keys and values of the keys as below
var variableObject=payload.variables[0];
var d = Object.keys(variableObject)[i];
var val=variableObject[d];
Object.keys(variableObject) gives the array of all tags available in the json object
Regards,
Sreedhar