looping a JSON object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2018 07:11 AM
Hi,
Im trying to apply a loop for a json object and i don't know how to access to JSON element.
this is my code.
var ticketJson = {"type" : "Changement1" , "etat" : "Approuvé","epiclink": "URL","priorite" : "P1"};
//var obj = JSON.parse(ticketJson);
var item = this.newItem();
item.setAttribute('value', ticketJson);
var values = this.getParameter('sysparm_values').split(',');
for(var i in values){
var rec = new GlideRecord('u_tickets_jira');
rec.initialize();
rec.parent = taskID;
rec.u_change = taskID;
rec.u_type = values[i].type;
rec.u_etat = values[i].etat;
rec.insert();
}
the code market in red color doesn't work it does't return the value of "type" and "etat" existing in json object.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2018 07:26 AM
rec.u_type = ticketJson.type;
rec.u_etat = ticketJson.etat;
Above should you help you.
If you are passing Object Array to Client , you can do JSON.stringify(jsonObj) and send to clent scirpt
At client end, you can then convert it back to json object using JSON.parse(jsonString);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2018 02:11 AM
ticketJson is in another function so i passe it in another var and i call it from this variable that names is "value".
and i need to access to this values in loop function
for (var i in values){
var rec = new GlideRecord('u_tickets_jira');
rec.initialize();
rec.parent = taskID;
rec.u_change = taskID;
rec.u_type = values[i]"here i want to access to the type";
rec.u_etat = values[i]"here i want to access to etat";
rec.insert();
}
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2018 11:14 PM
Hi,
Your for ( var i in values ) should get replaced with for ( var i = 0 ; i < values.length; i++ ) // values is JSON Object Array Collection.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2018 07:34 AM
Why not simply do something like:
var ticketJson = {"type" : "Changement1" , "etat" : "Approuvé","epiclink": "URL","priorite" : "P1"};
for (var key in ticketJson)
gs.print("Key is: " + key + " and value is: " + ticketJson[key])
Output:
[0:00:00.000] Script completed in scope global: script
*** Script: Key is: type and value is: Changement1
*** Script: Key is: etat and value is: Approuvé
*** Script: Key is: epiclink and value is: URL
*** Script: Key is: priorite and value is: P1