This is a sample only with a hardcoded JSON String.
1.) Add Rulefunction below to your project
//=====================================================================================
String[] rulefunction RuleFunctions.rfJsonRetAllArrayCodeElementsUsingArrayList {
scope {
String pjsonStr;
String pjsonNodename;
String pjsonElement;
}
body {
Object jsonNode = JSON.parseJSON(pjsonStr);
Object jsonNodeError =JSON.findValueNode(jsonNode,pjsonNodename);
Object itJsonFields = JSON.getAllElements(jsonNodeError);
int i=0;
Object oValueList=Collections.List.createArrayList();
while(Collections.Iterator.hasNext(itJsonFields)) {
Object element = Collections.Iterator.next(itJsonFields);
Object fieldMap = JSON.getFields(element);
while(Collections.Iterator.hasNext(fieldMap))
{
Object fieldEntry = Collections.Iterator.next(fieldMap);
//System.debugOut("### " + Collections.Map.Entry.getKey(fieldEntry) + " :" + Collections.Map.Entry.getValue(fieldEntry));
if (Collections.Map.Entry.getKey(fieldEntry)==pjsonElement) {
System.debugOut("### Array node " + pjsonNodename + "found value for element "+ pjsonElement + "=" + Collections.Map.Entry.getValue(fieldEntry));
Collections.List.add(oValueList,i,Collections.Map.Entry.getValue(fieldEntry));
i++;
}
}
}
String[] sArrRet=String[i]{};
for (int iCnt=0;iCnt<i;iCnt++)
sArrRet[iCnt]=Collections.List.get(oValueList,iCnt);
return sArrRet;
}
}
//=====================================================================================
2.)
To test the RuleFunction add code below to one of your Rules/Rulefunctions.
String jsonStr="{\"MainNode\": {\"ArrayNode\": [{\"retValue\": \"Error_1\",\"message\": \"No data\"},{\"retValue\": \"Error_2\",\"message\": \"Data incomplete\"}],\"retValue\": \"NOT_OK\"}}";
String[] sRet = RuleFunctions.rfJsonRetAllArrayCodeElementsUsingArrayList(jsonStr,"ArrayNode","retValue");