arraysmuledataweavemule-studiomulesoft

Convert String to Array with numeric values only


I have the below dataweave code (maybe not the best one) to check if an array contains any non-numeric elements. It also check for the number of items in the array.

%dw 2.0
output json
fun isValidElements(arr: Array) =
    arr map ((item) -> item is Number and item != null) reduce ((acc, item) -> acc and item)
fun isValidArray(arr: Array) =
    (sizeOf(arr) > 0) and (isEmpty(arr) or isValidElements(arr))
fun flagInvalidArray(arr: Array) = isValidArray(arr)
var MyArray = [123,456,789]
---
{
    "arrayValid": flagInvalidArray(MyArray),
    "arraySize": if (sizeOf(MyArray) > 1) "Multiple" else "Single"
}

As you can see this checks for string and nulls and flags if any array is valid or not (It is considered valid if it contains only numeric items)

Now, I need to do this check for a string array. i.e. if my input is like this "[123,456,789]".

If I do a conversion like this read(MyArray,"application/json"), it works fine. However it fails when I have any other non-numeric value in the string array. Few examples of how my string array will look like:

"[ABC,123,456,789]"
"[123,null,456]"
"[,,,,,%$$$]" -- In cases where there are junk values, we can make the array as empty and flag it as invalid

Edit: My requirement is to make sure that the array (in string format) that I have doesn't contain any non-numeric items. For example: "[123,456,789]" and "[12345]" are valid arrays. If I get any non-numeric characters inside the array or if it's an empty array, I need to flag it as invalid. That's where I'm looking for a function. Examples of invalid array: "[123,ABC,456]", "["ABC",456]", [], [123,456,null]


Solution

  • If you are trying to validate that all the elements of an array comply with a condition it is easy to implement that with the every() function of the Arrays module.

    I implemented this function with the condition that the array must no be empty and every element is a number. You can modify the condition as needed. I used the try()/orElse() first to read the input string as JSON. This will work only because a JSON numeric array matches your inputs, but it doesn't seem your inputs are really JSON.

    I added some example inputs to show the results.

    %dw 2.0
    output json
    import * from dw::core::Arrays
    import * from dw::Runtime
    
    fun isValidNumericArray(arr: Array) =
        !isEmpty(arr) and (arr every ((item) -> item is Number))
    
    fun isStringNumericArray(s: String) = 
        try(() -> isValidNumericArray(read(s,"application/json"))) orElse false
     
    ---
    {
        "valid": isStringNumericArray( "[123,456,789]" ),
        "emptyArray": isStringNumericArray( "[]" ),
        "nullItem": isStringNumericArray( "[123,null,456]" ),
        "string": isStringNumericArray( "[ABC,123,456,789]" ),
        "junk": isStringNumericArray("[,,,,,%\$]")
    }
    

    Output:

    {
      "valid": true,
      "emptyArray": false,
      "nullItem": false,
      "string": false,
      "junk": false
    }