The CreatorCon Call for Content is officially open! Get started here.

how to can i replace a json value before sending in rest message

Davie
Giga Expert

Hi good people

I’m hoping one of you maybe able to help with this

I need to force change a value of a before sending a payload

Here is an example of the payload, var = body represents the below.  The below string to find an update is "customfield_10352":{"value":"SDI - App - iOS"}

 

{"fields":{"customfield_10208":"INC0155815","customfield_10344":"TEST:4bb1edba1b069058d39b646fad4bcb35","customfield_10345":"https://blah.service-now.com/nav_to.do?uri=incident.do?sys_id=4bb1edba1b069058d313219b646fad4bcb35&sysparm_stack=incident_list.do?sysparm_query=active=true","customfield_10348":{"value":"SNOW Incident"},"customfield_10352":{"value":"SDI - App - iOS"},"customfield_10353":"Hairi Yahya","customfield_10355":"Bob Bob","customfield_10356":{"value":"Live"},"customfield_10357":"Platform)","customfield_10358":"","customfield_10359":"","customfield_10360":"","customfield_10366":"name name","customfield_10376":"Operational","customfield_10377":" SA","customfield_10379":"Assigned","customfield_10386":"Enter a summary of the subscriber ","customfield_10387":"","customfield_10389":{"value":"Company"},"customfield_10390":"Monitoring Alert","customfield_10412":{"value":"Incident"},"customfield_11106":"","description":" SDSDS \r\n   ","priority":{"id":"10003"},"summary":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.....4"}}

 

I want to force change this to SNOW-Only-Escalation_OR_DeEscalated_Request

The script include I’m working with is this.  The issue passed in is the data above before formatted into JSON, the highlighted is my attempt to find and replace then return the info to be sent in the rest message :

     modifyIssue: function(issue, key) {

                             var json = new JSON();

                      var body = json.encode(issue);

                            

function findAndReplace(body, value, replacevalue){

for(var x in object){

    if(typeof object[x] == typeof {}){

      findAndReplace(object[x], value, replacevalue);

    }

    if(object[x] == value){

      object["customfield_10352"] = replacevalue;

          }

  }

}

                             findAndReplace(body, "customfield_10352", "SNOW-Only-Escalation_OR_DeEscalated_Request");

                                                          var issuebody = body;

 

  var r = new RESTMessage('JIssue', 'put');

        r.setBasicAuth(this.username, this.password);

        this.debug("BODY to be Submitted: \n" + body);

        r.setXMLParameter('issuebody', body);

        r.setStringParameter('base_endpoint', this.grInt.u_ticket_url);

        r.setStringParameter("issueKey", key);

        var res = this._submitBodyTypeRequest(r, true, body, 'PUT');

        return res;

    },

 

 

1 ACCEPTED SOLUTION

maheshn
Mega Expert

Hi Davie,

Use below code it should resolve your issue.

var body = {"fields":{"customfield_10208":"INC0155815","customfield_10344":"TEST:4bb1edba1b069058d39b646fad4bcb35","customfield_10345":"https://blah.service-now.com/nav_to.do?uri=incident.do?sys_id=4bb1edba1b069058d313219b646fad4bcb35&sysparm_stack=incident_list.do?sysparm_query=active=true","customfield_10348":{"value":"SNOW Incident"},"customfield_10352":{"value":"SDI - App - iOS"},"customfield_10353":"Hairi Yahya","customfield_10355":"Bob Bob","customfield_10356":{"value":"Live"},"customfield_10357":"Platform)","customfield_10358":"","customfield_10359":"","customfield_10360":"","customfield_10366":"name name","customfield_10376":"Operational","customfield_10377":" SA","customfield_10379":"Assigned","customfield_10386":"Enter a summary of the subscriber ","customfield_10387":"","customfield_10389":{"value":"Company"},"customfield_10390":"Monitoring Alert","customfield_10412":{"value":"Incident"},"customfield_11106":"","description":" SDSDS \r\n ","priority":{"id":"10003"},"summary":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.....4"}};

body.fields.customfield_10352.value = 'SNOW-Only-Escalation_OR_DeEscalated_Request';

var issuebody = JSON.stringify(body); 

View solution in original post

3 REPLIES 3

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Davie,

You may find below link helpful.

https://stackoverflow.com/questions/4553235/how-to-change-json-keyvalue

 

- Pradeep Sharma

hmmm, i tried this from the link but that doesn't appear to work, i got this returned

findAndReplace triggered[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

 

script inc:

modifyIssue: function(issue, key) {

var json = [{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" },
{ "id": "5009", "type": "Juice" }];

function replaceByValue( field, oldvalue, newvalue ) {
for( var k = 0; k < json.length; ++k ) {
if( oldvalue == json[k][field] ) {
json[k][field] = newvalue ;
}
}
return json;
}
replaceByValue('id','5001');
replaceByValue('type','Hello World');
// replaceByValue('customfield_10352','value');
// replaceByValue('value','SNOW-Only-Escalation_OR_DeEscalated_Request');

var r = new RESTMessage('Jira Issue', 'put');
gs.log("findAndReplace triggered" + json,'JiraIntegration-modifyissue');
r.setBasicAuth(this.username, this.password);
this.debug("BODY to be Submitted: \n" + json);
r.setXMLParameter('issuebody', json);
r.setStringParameter('base_endpoint', this.grInt.u_ticket_url);
r.setStringParameter("issueKey", key);
var res = this._submitBodyTypeRequest(r, true, json, 'PUT');
return res;
},

 

maheshn
Mega Expert

Hi Davie,

Use below code it should resolve your issue.

var body = {"fields":{"customfield_10208":"INC0155815","customfield_10344":"TEST:4bb1edba1b069058d39b646fad4bcb35","customfield_10345":"https://blah.service-now.com/nav_to.do?uri=incident.do?sys_id=4bb1edba1b069058d313219b646fad4bcb35&sysparm_stack=incident_list.do?sysparm_query=active=true","customfield_10348":{"value":"SNOW Incident"},"customfield_10352":{"value":"SDI - App - iOS"},"customfield_10353":"Hairi Yahya","customfield_10355":"Bob Bob","customfield_10356":{"value":"Live"},"customfield_10357":"Platform)","customfield_10358":"","customfield_10359":"","customfield_10360":"","customfield_10366":"name name","customfield_10376":"Operational","customfield_10377":" SA","customfield_10379":"Assigned","customfield_10386":"Enter a summary of the subscriber ","customfield_10387":"","customfield_10389":{"value":"Company"},"customfield_10390":"Monitoring Alert","customfield_10412":{"value":"Incident"},"customfield_11106":"","description":" SDSDS \r\n ","priority":{"id":"10003"},"summary":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.....4"}};

body.fields.customfield_10352.value = 'SNOW-Only-Escalation_OR_DeEscalated_Request';

var issuebody = JSON.stringify(body);