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

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

I think you want to use JSON.parse and access as a javascript object.   It will honor the arrays in the notation and allow you to iterate as necessary:



var json = '{"mainCIid":"0b5999ee37f432007deb98a543990e14","roles":[{"perimeterIds":"e4c389cc0fd731006c18929792050e42,dace047f0f7852406c18929792050e8b","perimeterNames":"String 1, String 2"}]}';



jsObject = JSON.parse(json);



//now your objects should be there


alert(jsObject.mainCIid + ' ' + jsObject.roles[0].perimeterIds);


For the second part (sorry, I didn't read thoroughly...still early for me):



var json = '{"mainCIid":"0b5999ee37f432007deb98a543990e14","roles":[{"perimeterIds":"e4c389cc0fd731006c18929792050e42,dace047f0f7852406c18929792050e8b","perimeterNames":"String 1, String 2"}]}';



jsObject = JSON.parse(json);



var nameValuePairs = {};



var names = jsObject.roles[0].perimeterNames.split(',');


var values = jsObject.roles[0].perimeterIds.split(',');



for (var i = 0; i < names.length; i++) {


      nameValuePairs[names[i].trim()] = values[i].trim();


}



alert(nameValuePairs['String 1'] + ' ' + nameValuePairs['String 2']);


Hi,



Same problem when perimeterNames contains 2 strings : first is   'String 1 Part 1, String 1 Part 2' and second is 'String 2'


We have



var json = '{"mainCIid":"0b5999ee37f432007deb98a543990e14","roles":[{"perimeterIds":"e4c389cc0fd731006c18929792050e42,dace047f0f7852406c18929792050e8b","perimeterNames":"String 1 Part 1, String 1 Part 2, String 2"}]}';



and


var names = jsObject.roles[0].perimeterNames.split(',')



gives an array of 3 elements ['String 1 Part 1', 'String 1 Part 2, 'String 2'] but I would like only 2 elements



I think that I need to modify creation of json object on the server side but how ?


Joe McCarty1
ServiceNow Employee
ServiceNow Employee

Sounds like you need a more robust json object model.   Do have control over what is producing the JSON (i.e. script include) or is it coming from somewhere else?   If so, this will allow you to use commas and avoid split altogether simply by iterating through the roles array:


var json = '{"mainCIid":"0b5999ee37f432007deb98a543990e14","roles":[{"name":"String 1, Part 1 String 1, Part 2", "value":"e4c389cc0fd731006c18929792050e42"},{"name":"String 2", "value":"dace047f0f7852406c18929792050e8b"}]}';



jsObject = JSON.parse(json);



alert(jsObject.roles[0]['name'] + ' = ' + jsObject.roles[0]['value'] + ';' + jsObject.roles[1]['name'] + ' = ' + jsObject.roles[1]['value']);