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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sreedhar,



You can get the keys from json object. But you need to know value of which key you require and you want to parse then only you can retrieve value for that.



Can you post your json here and tell what you want to fetch/retrieve from that.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,



Below is the format of JSON which we will receive from another SNOW instance.


Payload:


{


    "u_number":"RITMXXXXX",


    "u_request_number":"REQXXXXX",


    "u_description":"",


    "u_short_description":"XXXXX",


    "u_cat_item":"XXXXX",


    "u_state":"2",


    "u_work_notes":"",


    "u_assignment_group_name":"XXXXXX",


    "u_assigned_to_user_name":"",


    "u_due_date":"2016-08-11 02:43:36",


    "u_quantity":"1",


    "variables":[


          {


                "u_requested_by":"XXXXX",


                "u_requested_on_behalf":"",


                "mac_reqtype":"XXXXXX",


          }


    ]


}



we have to do a bi direction integration between 2 SNOW instance for Service requests for which we would be receiving request payload from one SNOW instance wherein we will receive the details of cat item and its respective variables in above format.


Also different cat items will have different variables which will be coming in the payload.


My approach is to read the variables coming in payload and then use them for ordering an item in another SNOW instance.


Please let me know if the approach I am trying it correct or guide me with a appropriate approach.



Thanks for your response!


Deepak Ingale1
Mega Sage

Hi Sreedhar



You can loop through object



eg, if your json is something like   var myObj =   {"name": "ian", "last_name":"Json"} , then you can use



for ( var key in myObj) { gs.print(" key is : "   + key + "   and value for key is   " + myObj[key]);}



Also, if you want name key value, then you can access it my myObj["name"]



If your JSON is array of objects, then you need to use array iterator over the length property of the JSON


Hi Deepak,



From the above payload I want the values of u_requested_by and mac_reqtype.



I tried as u suggested but I am unable to get the values of these elements. Instead i cud get these elements Please find below script which i used



var l=new JSON().getKeys(obj.variables[0]);


for ( var key in l) {


gs.addInfoMessage(" key is : "   + key + "   and value for key is   " + l[key]);


gs.addInfoMessage("key value: "+obj(l[key]))


}



key value is returning undefined but I need the value of the key



Thanks