Aggregation Response parsing

Hi,
I am working on a Webservice Application that returns response as below
How can I parse the results so that I only get productId that have checked==true.

appProducts[*].productId gives all, but i need only the ones that have checked==true

"appProducts": [
                {
                    "checked": true,
                    "productId": 1102,
                    "productName": "Demand Management"
                },
                {
                    "checked": false,
                    "productDesc": "Requires Demand Management",
                    "productId": 10456,
                    "productName": "Portfolio Management"
                },
                {
                    "checked": false,
                    "productDesc": "Requires Demand Management and Project Management",
                    "productId": 10454,
                    "productName": "Program Management"
                }
]

Hi @udaya1,

If the webservice allow filter, you can use it. If it not possible, you can use an after operation rule where you read the response and return only the elements that you want.

Hi @enistri_devo The webserivce does not allow filtering.
Is there any sample code on the After rule to skip elements?

Here’s snippet of WebServiceBeforeOperationRule logic. You’ll have to modify the rule to suit your requirements.

// the usual imports and other goes above

 Object appProductsObj = processedResponseObject.get("appProducts");

 if (appProductsObj instanceof List) {
        List appProducts = (List) appProductsObj;

        for (Object itemObj : appProducts) {
            if (!(itemObj instanceof Map)) {
                continue; 
            }

            Map itemMap = (Map) itemObj;
            Object checkedObj = itemMap.get("checked");

            if (!Boolean.TRUE.equals(checkedObj)) {
                continue; // Skip items where checked != true
            }

            filteredAppProducts.add(itemMap);
        }

        // Replace appProducts with filtered list
        processedResponseObject.put("appProducts", filteredAppProducts);
    } else {
        // If appProducts is missing or invalid, empty list is returned
        processedResponseObject.put("appProducts", filteredAppProducts);
    }

    // Return updated processedResponseObject
    return processedResponseObject;
}
}
}

1 Like

The JSONPath for the filter is “$.appProducts[[email protected]==true]”