- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2022 10:42 AM
Hello Team
I am runing below mention API and getting the output in JSON format and want to get the sysid. How can i get it ?
"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.
Solved! Go to Solution.
- Labels:
-
Delegated Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2022 08:24 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2022 11:57 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2022 08:24 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2022 01:26 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2022 11:08 AM
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);