Map the json array in to string field with comma separated value

salu
Mega Guru

Hello,

I want to map the value from the json array to string variable.

[{"number":0,"fields":[{"min":6,"max":10,"label":"","type":"text","value":"111111"}]},{"number":1,"fields":[{"min":6,"max":10,"label":"","type":"text","value":"111111"}]}]

var json_output=this.getParameter('sysparm_json_output');

var parser = new JSONParser();

  var parsedData = parser.parse(json_output);

  var length = parsedData.length; // get length

  for(var i=0;i<length;i++)

  {

  var value1 = parsedData[i].fields[0].value;

  if(value1 != "")

  {

  inc.u_bdc_sku_number=value1.toString();// its not working

  }

  else

  break;

  }

1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage

Following should work fine



var json_output=this.getParameter('sysparm_json_output');



var parser = new JSONParser();


  var parsedData = parser.parse(json_output);



var strValue= '' ;



  var parsedData = parser.parse(json_output);



  var length = parsedData.length; // get length



  for(var i=0;i<length;i++)


  {


  var value1 = parsedData[i].fields[0].value;


  if(value1 != "")


  {


strValue += ','+value1;


  }


  }


strValue = strValue.substring(1);



  inc.u_bdc_sku_number=strValue ;// its not working


View solution in original post

15 REPLIES 15

Hi Saranya,



You could make use of array utils in here. There is a function unique to remove duplicates.


http://wiki.servicenow.com/index.php?title=ArrayUtil  


ArrayUtil unique odd behavior


Following could be used to check numeric number.A little modification may be required to use code server side


Client side number validation


gurpreet



The mapping of field are not correct in the above loop.



[{"number":0,"fields":[{"min":6,"max":10,"label":"","type":"text","value":"1234567890"}]},{"number":1,"fields":[{"min":6,"max":10,"label":"","type":"text","value":"234567890"}]}]



But its mapping like this 234567890,234567890



[{"number":0,"fields":[{"min":6,"max":10,"label":"","type":"text","value":"1234567890"}]},{"number":1,"fields":[{"min":6,"max":10,"label":"","type":"text","value":"0987654321"}]},{"number":2,"fields":[{"min":6,"max":10,"label":"","type":"text","value":"1231231231"}]}]



Its mapping like 34567890,0987654321,1231231231




First value is not taking correctly




thanks


saranya


Hi Saranya,



Its seems like you have used instruction twice     strValue = strValue.substring(1);       OR there may be some issue with     break;   instruction. Please paste your code in here any i will try to troubleshoot the issue.



Hello Gurpreet,



Here I want to insert that value in the other table of the incident record as well as I need to map that value1 in one string field with comma separated.



var parser = new JSONParser();


  var strValue1= '' ;



  var parsedData = parser.parse(json_output);



  var length = parsedData.length; // get length



  for(var i=0;i<length;i++)


  {


  var value1 = parsedData[i].fields[0].value;


  if(value1 != "")


  {


  strValue1 += ','+value1;


  var gr = new GlideRecord('u_bdc_sku_details');


  gr.initialize();


  gs.log('Jan 17 inside if '+strValue);


  gr.u_bdc_sku =value1.toString();


  gr.u_incident=inc.sys_id;


  gr.insert();


  }


  strValue1 = strValue1.substring(1);


  }


  inc.u_bdc_sku_number = strValue1;


  inc.update();


gurpreet


Hello Gurpreet,



I got the issue.


Now its working .


Thank you very much for the help




var parser = new JSONParser();


  var strValue1= '' ;



  var parsedData = parser.parse(json_output);



  var length = parsedData.length; // get length



  for(var i=0;i<length;i++)


  {


  var value1 = parsedData[i].fields[0].value;


  if(value1 != "")


  {


  strValue1 += ','+value1;


  var gr = new GlideRecord('u_bdc_sku_details');


  gr.initialize();


  gs.log('Jan 17 inside if '+strValue);


  gr.u_bdc_sku =value1.toString();


  gr.u_incident=inc.sys_id;


  gr.insert();


  }



  }


  strValue1 = strValue1.substring(1);


  inc.u_bdc_sku_number = strValue1;


  inc.update();