Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help to transform just one property of array object into string

Flavio Tiezzi
Kilo Sage

Hello guys,

I need help to transform just one property of array object into string. How can I achieve this?

var obj = [{
           "name": "John",
          "board_order": 1,
	  },
          {
          "name": "Paul",
          "board_order": 2,
          }];

//To this:

var obj = [{
          "name": "John",
          "board_order": "1",
	  },
          {
           "name": "Paul",
          "board_order": "2",
          }];

Thanks in advance.

4 REPLIES 4

Sagar Pagar
Tera Patron

Hi,

Try this -

const myJSON = JSON.stringify(obj);

gs.info(myJSON+ "\n\n");

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi,

Unfortunately it didn't work!

Thanks!

Muhammad Khan
Mega Sage

Hi @Flavio Tiezzi 

Try this

var obj = [{
           "name": "John",
          "board_order": 1,
	  },
          {
          "name": "Paul",
          "board_order": 2,
          }];

for(var i=0; i<obj.length; i++){
    obj[i].board_order = obj[i].board_order.toString();
}

Sal12
Tera Expert

@Flavio Tiezzi 

This works for me. You could try it on Scripts - Background ( All menu > System Definition > Scripts - Background)

var obj = [{
           "name": "John",
          "board_order": 1,
      },
          {
          "name": "Paul",
          "board_order": 2,
          }];
          
gs.info(JSON.stringify(obj));  
// [{"name":"John","board_order":1},{"name":"Paul","board_order":2}]

for (var i in obj){
  obj[i]["board_order"] =  obj[i]["board_order"] + ""; 
}

gs.info(JSON.stringify(obj));  
// [{"name":"John","board_order":"1"},{"name":"Paul","board_order":"2"}]