JSON string manipulation - delete element(s)

poyntzj
Kilo Sage

Hi all

I need to modify a page - category picker for the KB articles.

I have tracked down the relevant ui page and macros and I can see that a JSON string is returned

{"id":"351b0b2c6ff482000bd2129e5d3ee4a6","items":[],"label":"Announcements"},{"id":"432b0b2c6ff482000bd2129e5d3ee4dc","items":[],"label":"FAQ"},{"id":"cd3b832c6ff482000bd2129e5d3ee4d7","items":[],"label":"Known Error"},{"id":"4a4b0b2c6ff482000bd2129e5d3ee431","items":[],"label":"Post Incident Reports"},{"id":"8a5b872c6ff482000bd2129e5d3ee470","items":[],"label":"Procedure"},{"id":"3b5b4b2c6ff482000bd2129e5d3ee4ae","items":[],"label":"System Documentation"},{"id":"b36b4b2c6ff482000bd2129e5d3ee484","items":[],"label":"User Guides"},{"id":"177b872c6ff482000bd2129e5d3ee420","items":[],"label":"Workaround"}

what I need to do is to determine if the user is a member of a group or not, and if not, remove one or two of the elements from the JSON string.

I thought it would be an array, or an existing JSON util would work, but I have not see anything so far.

i saw this What is the proper way to serialize / deserialize objects?

and I have followed that in that I have the code

var j= new JSON()

var x = j.encode({"id":"351b0b2c6ff482000bd2129e5d3ee4a6","items":[],"label":"Announcements"},{"id":"432b0b2c6ff482000bd2129e5d3ee4dc","items":[],"label":"FAQ"},{"id":"cd3b832c6ff482000bd2129e5d3ee4d7","items":[],"label":"Known Error"},{"id":"4a4b0b2c6ff482000bd2129e5d3ee431","items":[],"label":"Post Incident Reports"},{"id":"8a5b872c6ff482000bd2129e5d3ee470","items":[],"label":"Procedure"},{"id":"3b5b4b2c6ff482000bd2129e5d3ee4ae","items":[],"label":"System Documentation"},{"id":"b36b4b2c6ff482000bd2129e5d3ee484","items":[],"label":"User Guides"},{"id":"177b872c6ff482000bd2129e5d3ee420","items":[],"label":"Workaround"});

var y = j.decode(x);

gs.log(y.id);

and that returns me the ID for the first record. - on the right track I thought

however, I cannot seem to loop through and find the others

for example, gs.log(y.id[1]) I was hoping would give me the next id, and all I get is 5

Anyone got any ideas ?

Thanks in Advance

1 ACCEPTED SOLUTION

ended up by calling a Script Include from the ui macro



so the ui macro determines if the user is in any of a few groups and if so calls this script include.


this returns the revised JSON string with the element removed


the category picker then does not show the option


there is a also a reference qualifier running so the user cannot just type in the name either)



function kbRemoveAnnouncements(jsonStr)


{


  //gs.log('kbRemoveAnnouncements : ' + jsonStr);


  var j = new JSON();


  var jd = j.decode(jsonStr);


  for (i=0;i<jd.length;i++)


  {


  if (jd[i].label.toLowerCase() == 'announcements')


  var intRemove = i;


  break;


  }


  delete jd[intRemove];


  var jsonStr = j.encode(jd);


  return jsonStr;


}


View solution in original post

2 REPLIES 2

poyntzj
Kilo Sage

I have made some progress... I think


I now have the followig code which I can see is deleting the element, but the array still stays at the same length and when I print the results of the object, where I have removed an element I see two commas and if I try to show the element's data I get undefined (so it is removed)


Is that right - seems a little untidy.



I will test in some code tomorrow to see what it does



var data = {"result":[{"id":"351b0b2c6ff482000bd2129e5d3ee4a6","items":[],"label":"Announcements"},{"id":"432b0b2c6ff482000bd2129e5d3ee4dc","items":[],"label":"FAQ"},{"id":"cd3b832c6ff482000bd2129e5d3ee4d7","items":[],"label":"Known Error"},{"id":"4a4b0b2c6ff482000bd2129e5d3ee431","items":[],"label":"Post Incident Reports"},{"id":"8a5b872c6ff482000bd2129e5d3ee470","items":[],"label":"Procedure"},{"id":"3b5b4b2c6ff482000bd2129e5d3ee4ae","items":[],"label":"System Documentation"},{"id":"b36b4b2c6ff482000bd2129e5d3ee484","items":[],"label":"User Guides"},{"id":"177b872c6ff482000bd2129e5d3ee420","items":[],"label":"Workaround"}]


}



gs.log(data.result)


gs.log(data.result.length)



for (i=0;i<data.result.length;i++)


gs.log(data.result[i].id + ' : ' + data.result[i].label)



gs.log('----------------');


for (i=0;i<data.result.length;i++)


{


gs.log(data.result[i].id + ' : ' + data.result[i].label)


if (data.result[i].label == 'Procedure')


{


var intRemove = i


break;


}


}


gs.log('----------------');




gs.print('removing : ' + intRemove)


delete data.result[intRemove]


gs.log('----------------');




gs.log(data.result)


gs.log(data.result.length)



for (i=0;i<data.result.length;i++)


gs.log(data.result[i].id + ' : ' + data.result[i].label)



which gets me


*** Script: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]


*** Script: 8


*** Script: 351b0b2c6ff482000bd2129e5d3ee4a6 : Announcements


*** Script: 432b0b2c6ff482000bd2129e5d3ee4dc : FAQ


*** Script: cd3b832c6ff482000bd2129e5d3ee4d7 : Known Error


*** Script: 4a4b0b2c6ff482000bd2129e5d3ee431 : Post Incident Reports


*** Script: 8a5b872c6ff482000bd2129e5d3ee470 : Procedure


*** Script: 3b5b4b2c6ff482000bd2129e5d3ee4ae : System Documentation


*** Script: b36b4b2c6ff482000bd2129e5d3ee484 : User Guides


*** Script: 177b872c6ff482000bd2129e5d3ee420 : Workaround


*** Script: ----------------


*** Script: 351b0b2c6ff482000bd2129e5d3ee4a6 : Announcements


*** Script: 432b0b2c6ff482000bd2129e5d3ee4dc : FAQ


*** Script: cd3b832c6ff482000bd2129e5d3ee4d7 : Known Error


*** Script: 4a4b0b2c6ff482000bd2129e5d3ee431 : Post Incident Reports


*** Script: 8a5b872c6ff482000bd2129e5d3ee470 : Procedure


*** Script: ----------------


*** Script: removing : 4


*** Script: ----------------


*** Script: [object Object],[object Object],[object Object],[object Object],,[object Object],[object Object],[object Object]


*** Script: 8


*** Script: 351b0b2c6ff482000bd2129e5d3ee4a6 : Announcements


*** Script: 432b0b2c6ff482000bd2129e5d3ee4dc : FAQ


*** Script: cd3b832c6ff482000bd2129e5d3ee4d7 : Known Error


*** Script: 4a4b0b2c6ff482000bd2129e5d3ee431 : Post Incident Reports


*** Script: undefined : undefined


*** Script: 3b5b4b2c6ff482000bd2129e5d3ee4ae : System Documentation


*** Script: b36b4b2c6ff482000bd2129e5d3ee484 : User Guides


*** Script: 177b872c6ff482000bd2129e5d3ee420 : Workaround


ended up by calling a Script Include from the ui macro



so the ui macro determines if the user is in any of a few groups and if so calls this script include.


this returns the revised JSON string with the element removed


the category picker then does not show the option


there is a also a reference qualifier running so the user cannot just type in the name either)



function kbRemoveAnnouncements(jsonStr)


{


  //gs.log('kbRemoveAnnouncements : ' + jsonStr);


  var j = new JSON();


  var jd = j.decode(jsonStr);


  for (i=0;i<jd.length;i++)


  {


  if (jd[i].label.toLowerCase() == 'announcements')


  var intRemove = i;


  break;


  }


  delete jd[intRemove];


  var jsonStr = j.encode(jd);


  return jsonStr;


}