Read values from json object using keys

sreedharkaliset
Mega Expert

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...

12 REPLIES 12

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


JSON.stringify()


JSON.parse()


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


divya mishra
Tera Guru

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


prathyusha_snow
Tera Contributor

Hello Sreedhar,



Please let me know if you could get the values printed. I'm having a similar issue.


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