VMware Cloud Community
getvrohelp
Enthusiast
Enthusiast
Jump to solution

Parse JSON excluding anything with X in a value - I need a contains not a match or ==

So I will do my best to explain the situation. I have an action that makes an API call to LogicMonitor and returns an array of properties. This is so I can have a searchable multi value picker on the SB form. This returns 3 objects per LogicMonitor device group. The fullpath, the ID and the description. What I need to be able to do is filter out  and exclude any that have "DO NOT ADD" in the description and return the id and fullpath only as an array of properties. Again, I have the array of properties returning just fine with the id and the fullpath. I was told today to filter out anything that has "DO NOT ADD" in the description. I have my code below and I'll provide some data to go with in in the json response that can be used as dummy data. I hope this is doable as I will have to also filter out later any that start with "Application". I would appreciate any help very very much. Thanks in advance.

 

 

 

 

var responseString = '{"total":405,"items":[{"id":1024,"description":"ASF provides a rich set of proven drivers and code modules developed by experts to reduce design time. It simplifies the usage of microcontrollers by providing an abstraction to the hardware through drivers and high-value middlewares.","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ULM - Name"},{"id":1,"description":"# DO NOT ADD DEVICES AT THIS GROUP LEVEL","fullPath":""},{"id":1025,"description":"","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/UKW - Name"},{"id":1026,"description":"","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/UKC - Name"},{"id":1027,"description":"# DO NOT ADD","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/TEL - Teltow"},{"id":1028,"description":"# DO NOT ADD DEVICES AT THIS GROUP LEVEL","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ROU - Rousset"},{"id":1029,"description":"All of the Windows-based DHCP systems within Microchip.","fullPath":"Application Groups/IS - Information Systems/Infrastructure & Business Integration/Windows Infrastructure/DHCP Systems"},{"id":1021,"description":"","fullPath":"Application Groups/Backend Operations/EA - Enterprise Applications/Microsoft SQL Systems/AZP - Phoenix","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ROB - Name"}]}'
//Code

var deviceGroupNameArray = new Properties();

var responseJson = JSON.parse(responseString);
var deviceGroupNameJsonResponseArray = responseJson['items'];
System.debug("responseJson is: \n" + JSON.stringify(responseJson, null, 4));

if (responseJson) {
    for (i in deviceGroupNameJsonResponseArray) {
        var deviceGroupDescription = deviceGroupNameJsonResponseArray[i].description;
        System.debug("deviceGroupDescription : " + JSON.stringify(deviceGroupDescription));
        var deviceGroupId = deviceGroupNameJsonResponseArray[i].id;
        System.debug("deviceGroupId : " + JSON.stringify(deviceGroupId));
        var deviceGroupFullPath = deviceGroupNameJsonResponseArray[i].fullPath;
        System.debug("deviceGroupfullPath : " + JSON.stringify(deviceGroupFullPath));
        var deviceDescString = JSON.stringify(deviceGroupDescription).toLocaleLowerCase();
        System.warn(deviceDescString);
        deviceGroupNameArray.put(deviceGroupFullPath, deviceGroupId);
    }
}

var tempDataArray = new Array();
for each (key in deviceGroupNameArray.keys) {
    tempDataArray.push(deviceGroupNameArray.get(key) + "@@" + key);
}

var sortedDeviceGroupNamePropertiesArray = new Array();
for each (item in tempDataArray.sort()) {
     var prop = new Properties();
     prop.put("label", item.split("@@")[1]);
     prop.put("value", item.split("@@")[0]);
     sortedDeviceGroupNamePropertiesArray.push(prop);
     System.debug(item.split("@@")[0]);
     System.debug(item.split("@@")[1]);
}

return sortedDeviceGroupNamePropertiesArray; // properties array

 

 

 

 

My variable names suck I know :).

0 Kudos
1 Solution

Accepted Solutions
StefanSchnell
Enthusiast
Enthusiast
Jump to solution

Hello @getvrohelp,

here an approach how to handle your requirement:

var responseString = '{"total":405,"items":[{"id":1024,"description":"ASF provides a rich set of proven drivers and code modules developed by experts to reduce design time. It simplifies the usage of microcontrollers by providing an abstraction to the hardware through drivers and high-value middlewares.","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ULM - Name"},{"id":1,"description":"# DO NOT ADD DEVICES AT THIS GROUP LEVEL","fullPath":""},{"id":1025,"description":"","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/UKW - Name"},{"id":1026,"description":"","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/UKC - Name"},{"id":1027,"description":"# DO NOT ADD","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/TEL - Teltow"},{"id":1028,"description":"# DO NOT ADD DEVICES AT THIS GROUP LEVEL","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ROU - Rousset"},{"id":1029,"description":"All of the Windows-based DHCP systems within Microchip.","fullPath":"Application Groups/IS - Information Systems/Infrastructure & Business Integration/Windows Infrastructure/DHCP Systems"},{"id":1021,"description":"","fullPath":"Application Groups/Backend Operations/EA - Enterprise Applications/Microsoft SQL Systems/AZP - Phoenix","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ROB - Name"}]}';

// Filters the elements which does not contain DO NOT ADD in the description
var result = JSON.parse(responseString).items.filter( function(item) {
  return (item.description.indexOf("DO NOT ADD") === -1);
});

// Adds id and fullPath to an array of arrays
var arrResult = [];
result.forEach( function(includedItems) {
  arrResult.push([includedItems.id, includedItems.fullPath]);
});

// Prints the id, at position [0], and the fullPath, at position [1]
arrResult.forEach( function(item) {
  System.log(item[0] + " - " + item[1]);
});

StefanSchnell_0-1704899611891.png

Hope this helps you.

Best regards
Stefan


More interesting information at blog.stschnell.de

View solution in original post

0 Kudos
3 Replies
getvrohelp
Enthusiast
Enthusiast
Jump to solution

I think I may have it. If there is a better way please let

if (responseJson) {
    for (i in deviceGroupNameJsonResponseArray) {
        var deviceGroupDescription = deviceGroupNameJsonResponseArray[i].description;
        var deviceDescString = JSON.stringify(deviceGroupDescription).toLocaleLowerCase();
        var searchString = "DO NOT ADD";
        var exclude = deviceDescString.indexOf(searchString.toLocaleLowerCase());
        System.debug("deviceGroupDescription : " + JSON.stringify(deviceGroupDescription));
        var deviceGroupId = deviceGroupNameJsonResponseArray[i].id;
        System.debug("deviceGroupId : " + JSON.stringify(deviceGroupId));
        var deviceGroupFullPath = deviceGroupNameJsonResponseArray[i].fullPath;
        System.debug("deviceGroupfullPath : " + JSON.stringify(deviceGroupFullPath));
        if (exclude == '-1') {
            deviceGroupNameArray.put(deviceGroupFullPath, deviceGroupId);
        }
    }
}

me know.

 

 

 

0 Kudos
StefanSchnell
Enthusiast
Enthusiast
Jump to solution

Hello @getvrohelp,

here an approach how to handle your requirement:

var responseString = '{"total":405,"items":[{"id":1024,"description":"ASF provides a rich set of proven drivers and code modules developed by experts to reduce design time. It simplifies the usage of microcontrollers by providing an abstraction to the hardware through drivers and high-value middlewares.","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ULM - Name"},{"id":1,"description":"# DO NOT ADD DEVICES AT THIS GROUP LEVEL","fullPath":""},{"id":1025,"description":"","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/UKW - Name"},{"id":1026,"description":"","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/UKC - Name"},{"id":1027,"description":"# DO NOT ADD","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/TEL - Teltow"},{"id":1028,"description":"# DO NOT ADD DEVICES AT THIS GROUP LEVEL","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ROU - Rousset"},{"id":1029,"description":"All of the Windows-based DHCP systems within Microchip.","fullPath":"Application Groups/IS - Information Systems/Infrastructure & Business Integration/Windows Infrastructure/DHCP Systems"},{"id":1021,"description":"","fullPath":"Application Groups/Backend Operations/EA - Enterprise Applications/Microsoft SQL Systems/AZP - Phoenix","fullPath":"Devices by Region/EMEA - Europe-Middle East-Africa/ROB - Name"}]}';

// Filters the elements which does not contain DO NOT ADD in the description
var result = JSON.parse(responseString).items.filter( function(item) {
  return (item.description.indexOf("DO NOT ADD") === -1);
});

// Adds id and fullPath to an array of arrays
var arrResult = [];
result.forEach( function(includedItems) {
  arrResult.push([includedItems.id, includedItems.fullPath]);
});

// Prints the id, at position [0], and the fullPath, at position [1]
arrResult.forEach( function(item) {
  System.log(item[0] + " - " + item[1]);
});

StefanSchnell_0-1704899611891.png

Hope this helps you.

Best regards
Stefan


More interesting information at blog.stschnell.de

0 Kudos
getvrohelp
Enthusiast
Enthusiast
Jump to solution

Thank you very much. I really appreciate it.

0 Kudos