Processing json data : list of string with comma

pbo
Mega Expert

I need to process following type of json data in client script

{"mainCIid":"0b5999ee37f432007deb98a543990e14",

"roles":[

{"perimeterIds":"e4c389cc0fd731006c18929792050e42,dace047f0f7852406c18929792050e8b",

"perimeterNames":"String 1, String 2"}]}

perimeterIds and perimeterNames are extracted from a glide_list field named u_perimetre (reference to cmn_department table) on the server side using GlideRecord query using

role.perimeterIds=rolesCI.getValue('u_perimetre');

role.perimeterNames=rolesCI.getDisplayValue('u_perimetre');

So in this case

String1 is the display value of element with id e4c389cc0fd731006c18929792050e42

String2 is the display value of element with id dace047f0f7852406c18929792050e8b

I tried client script code with

  result.roles[0].perimetreIds.split(',') OK It gives an array of 2 Ids

  result.roles[0].split(',') It gives an array of names

But this doesn't work when any name contains a comma

In this case   result.roles[0].split(',') returns more than 2 names, for exemple if String 1 = String 1 Part 1 , String 1 Part 2 this creates an array of 3 elements

And I can't associate first Id to first name  

This will build

First Id (e4c389cc0fd731006c18929792050e42) --> String 1 Part 1

Second Id (dace047f0f7852406c18929792050e8b) --> String 1 Part 2

instead of

First Id (e4c389cc0fd731006c18929792050e42) --> String 1 Part 1 , String 1 Part 2

Second Id (dace047f0f7852406c18929792050e8b) --> String 2

What needs to be changed (server or client script) to achieve this ?

1 ACCEPTED SOLUTION

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

A simpler solution that wouldn't require you to re-layout the JSON object would be to change the perimiterNames delimiter to something deemed safe would be to replace the first 3 lines in your while loop with something like this (obviously I'm not able to test).   I think any direction you go will require you to requery the values from the glide list rather than relying on getDisplayValue:



var role = {};


role.perimeterIds=rolesCI.getValue('u_perimetre');



//Get names manually so we can override the default delimiter...


var perimeters = new GlideRecord('<name of table that the u_perimetre field points to>');


perimeters.addQuery('sys_id', 'IN', role.perimeterIds);


perimeters.query();



//Push the name output into an array...


var perimeterNames = [];



while (perimeters.next()) {


        perimeterNames.push(perimeters.<field name of the display value, usually 'name'> + '');


}



role.perimeterNames=perimeterNames.join('^'); //or whatever delimiter is safe



Alternately, if you wanted to retool the JSON a bit more:



var role = {};



//Get names manually so we can override the default delimiter...


var perimeters = new GlideRecord('<name of table that the u_perimetre field points to>');


perimeters.addQuery('sys_id', 'IN', rolesCI.getValue('u_perimetre'));


perimeters.query();



//Push the name output into an array of JSON objects


var perimetersArr = [];



while (perimeters.next()) {


  var perimeter = {}


        perimeter.name = perimeters.<field name of the display value, usually 'name'>;


        perimeter.id = perimeters.sys_id;


        perimetersArr.push(perimeter);


}



role.perimeters=perimetersArr;


I'm expecting it to produce something like this which should be pretty delimiter agnostic:


{  


    "mainCIid":"0b5999ee37f432007deb98a543990e14",


    "roles":[  


          {  


                "perimeters":[  


                      {  


                            "name":"String 1",


                            "id":"e4c389cc0fd731006c18929792050e42"


                      },


                      {  


                            "name":"String 2",


                            "id":"dace047f0f7852406c18929792050e8b"


                      }


                ]


          }


    ]


}


View solution in original post

8 REPLIES 8

Yes ,


Json is produced by an UI script that we may modify but I don't know how.



We extract data from a custom table (u_roles) with field (u_perimetre)


This field is a glide_list referencing cmn_department table



When we create the json object on the server side we use



var rolesCI = new GlideRecord('u_roles);


rolesCI.addQuery(someQuery)


rolesCI.query();



to retrieve a list of u_roles



then



Capture d



So basically perimetersIds is a list of cmn_department ids and perimetersNames is a list of cmn_department names.



To make sure that there's no comma in result of rolesCI.getDisplayValue('u_perimetre'), I need to get each display value of the glide_list element


Joe McCarty1
ServiceNow Employee
ServiceNow Employee

A simpler solution that wouldn't require you to re-layout the JSON object would be to change the perimiterNames delimiter to something deemed safe would be to replace the first 3 lines in your while loop with something like this (obviously I'm not able to test).   I think any direction you go will require you to requery the values from the glide list rather than relying on getDisplayValue:



var role = {};


role.perimeterIds=rolesCI.getValue('u_perimetre');



//Get names manually so we can override the default delimiter...


var perimeters = new GlideRecord('<name of table that the u_perimetre field points to>');


perimeters.addQuery('sys_id', 'IN', role.perimeterIds);


perimeters.query();



//Push the name output into an array...


var perimeterNames = [];



while (perimeters.next()) {


        perimeterNames.push(perimeters.<field name of the display value, usually 'name'> + '');


}



role.perimeterNames=perimeterNames.join('^'); //or whatever delimiter is safe



Alternately, if you wanted to retool the JSON a bit more:



var role = {};



//Get names manually so we can override the default delimiter...


var perimeters = new GlideRecord('<name of table that the u_perimetre field points to>');


perimeters.addQuery('sys_id', 'IN', rolesCI.getValue('u_perimetre'));


perimeters.query();



//Push the name output into an array of JSON objects


var perimetersArr = [];



while (perimeters.next()) {


  var perimeter = {}


        perimeter.name = perimeters.<field name of the display value, usually 'name'>;


        perimeter.id = perimeters.sys_id;


        perimetersArr.push(perimeter);


}



role.perimeters=perimetersArr;


I'm expecting it to produce something like this which should be pretty delimiter agnostic:


{  


    "mainCIid":"0b5999ee37f432007deb98a543990e14",


    "roles":[  


          {  


                "perimeters":[  


                      {  


                            "name":"String 1",


                            "id":"e4c389cc0fd731006c18929792050e42"


                      },


                      {  


                            "name":"String 2",


                            "id":"dace047f0f7852406c18929792050e8b"


                      }


                ]


          }


    ]


}


I modified server script with



while (perimeters.next()) {


        perimeterNames.push(perimeters.name + '');


}


role.perimeterNames=perimeterNames.join('|'); //or whatever delimiter is safe



and the client script to use perimeterNames.split('|')



Thanks


anurag92
Kilo Sage

I exhausted all known methods, but was unable to solve this tricky issue.



But, maybe you can try this:



Find the number of commas in perimeterNames, then split and find the length of created array.



So if there are 3 commas, the length will be 4. The last element will be definitely your second display value.



for 1st display value, you can create a for loop to add first (n-1) elements of the array,in a string, where n=length