@ Action ( name = "Array Size" , outputs = { @ Output ( OutputNames . RETURN_RESULT ) , @ Output ( OutputNames . RETURN_CODE ) , @ Output ( OutputNames . EXCEPTION ) } , responses = { @ Response ( text = ResponseNames . SUCCESS , field = OutputNames . RETURN_CODE , value = ReturnCodes . SUCCESS , matchType = MatchType . COMPARE_EQUAL , responseType = ResponseType . RESOLVED ) , @ Response ( text = ResponseNames . FAILURE , field = OutputNames . RETURN_CODE , value = ReturnCodes . FAILURE , matchType = MatchType . COMPARE_EQUAL , responseType = ResponseType . ERROR , isOnFail = true ) } ) public Map < String , String > execute ( @ Param ( value = Constants . InputNames . ARRAY , required = true ) String array ) { Map < String , String > returnResult = new HashMap <> ( ) ; if ( StringUtilities . isBlank ( array ) ) { return populateResult ( returnResult , new Exception ( NOT_A_VALID_JSON_ARRAY_MESSAGE ) ) ; } JsonNode jsonNode ; try { ObjectMapper mapper = new ObjectMapper ( ) ; jsonNode = mapper . readTree ( array ) ; } catch ( IOException exception ) { final String value = "Invalid jsonObject provided! " + exception . getMessage ( ) ; return populateResult ( returnResult , value , exception ) ; } final String result ; if ( jsonNode instanceof ArrayNode ) { final ArrayNode asJsonArray = ( ArrayNode ) jsonNode ; result = Integer . toString ( asJsonArray . size ( ) ) ; } else { return populateResult ( returnResult , new Exception ( NOT_A_VALID_JSON_ARRAY_MESSAGE ) ) ; } return populateResult ( returnResult , result , null ) ; }
This operation determines the number of elements in the given JSON array . If an element is itself another JSON array it only counts as 1 element ; in other words it will not expand and count embedded arrays . Null values are also considered to be an element .