How to get the sysID from o(JSON.stringify(JSON.parse(output), null, 2)) outPut

anilkumarsharma
Giga Guru

Hello Team

@Ankur Bawiskar 

I am runing below mention API and getting the output in JSON format and want to get the sysid. How can i get it ?

var payload = {
  "items": [
    {
      "className": "cmdb_ci",
      "values": {
        
       "ip_address":'11.11.23.45',
 
      }
    }
  ]
};
 
var input = JSON.stringify(payload);
var output = sn_cmdb.IdentificationEngine.identifyCI(input);
 
gs.info(JSON.stringify(JSON.parse(output), null, 2));
 
when i am running this code then below mention output is coming here.
 

 "items": [ { "className": "cmdb_ci", "operation": "UPDATE", "sysId": "90b9887097c2d110305238271153af1f", "identifierEntrySysId": "fde6803c9782d110305238271153af2f", "identificationAttempts": [ { "info": "sys_object_source SKIPPED", "attemptResult": "SKIPPED", "identifierName": "", "attributes": [], "hybridEntryCiAttributes": [] }, { "attemptResult": "MATCHED", "identifierName": "Configuration Item rule", "attributes": [ "ip_address" ], "hybridEntryCiAttributes": [], "searchOnTable": "cmdb_ci" } ], "info": [], "errorCount": 0, "inputIndices": [ 0 ], "mergedPayloadIds": [], "markers": [], "warningCount": 0 } ], "additionalCommittedItems": [], "relations": [], "additionalCommittedRelations": [], "hasError": false, "hasWarning": false }

 

How can i get the sysid from this Json format.? Please help.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

This is the updated json and this will work

var payload = '{"items":[{"className":"cmdb_ci","values":{"ip_address":"11.11.23.45"}}]}';

var parsed = JSON.parse(payload);


var ip = parsed.items[0].values["ip_address"];

regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Claude DAmico
Kilo Sage

From the object, not the string, you can get it like this:

var obj = {"items": [{"className": "cmdb_ci", "operation": "UPDATE", "sysId": "90b9887097c2d110305238271153af1f"}]};

gs.info(obj.items[0].sysId);

//Output: 90b9887097c2d110305238271153af1f

From the string, do this:

var obj = {"items": [{"className": "cmdb_ci", "operation": "UPDATE", "sysId": "90b9887097c2d110305238271153af1f"}]};
var jStr = JSON.stringify(obj);
var start = jStr.indexOf("sysId") + 8;
var end = start + 32;
var sysID = jStr.substring(start, end);

gs.info(sysID);

//Output: 90b9887097c2d110305238271153af1f
Claude E. D'Amico, III - CSA

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

This is the updated json and this will work

var payload = '{"items":[{"className":"cmdb_ci","values":{"ip_address":"11.11.23.45"}}]}';

var parsed = JSON.parse(payload);


var ip = parsed.items[0].values["ip_address"];

regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Sir,

I did make it lightly change which is working.

i have question why did you add 8 and 32 . Please explain.

var start = jStr.indexOf("sysId") + 8;
var end = start + 32;
var sysID = jStr.substring(start, end);

Below Solution is also working :

 

answer = (function transformEntry(source) {
var payload = {
  "items": [
    {
      "className": "cmdb_ci",
      "values": {
        
       "ip_address":'source.u_ip_address',
        "fqdn":"HHAA"

 


     }
    }
  ]
};
var input = JSON.stringify(payload);
var output = sn_cmdb.IdentificationEngine.identifyCI(input);
var obj = JSON.stringify(JSON.parse(output, function(key, value) {
if (key== 'sysId') {
gs.info('anilSysid: ' + value);
return value;
}
}), undefined, 2);