How to use try function with map in dataweave 2.0

Viewed 40

Hi everybody i hope you are well, i have a doubt how can i do to use the try inside map in dataweave, i explain about my issue, i receive a csv file with multiple rows, that rows are group by order first and the second, i use map to transform the data in json ( to join all the rows with the same order) format using a couple colums that colums coming from the csv file, if any colum has empty or null the map fail and broke all the content file, how can i use the try function in dataweave if any group of orders fail only get the order and put in another part of the json and follow with the next order without broke the loop.

Part of the CSV File - Demo:

number,date,upc,quantity,price
1234556,2022-08-04,4015,1,
1234556,2022-08-04,4019,1,2.00
1234556,2022-08-04,4016,1,3.00
1234557,2022-08-04,4015,1,3.00

Dataweave:

%dw 2.0
output application/json
---
payload groupBy ($.number) pluck $ map ( () -> {
   "number": $[0].number,
   "date": $[0].date,
   "items": $ map {
       "upc": $.upc,
       "price": $.price as Number {format: "##,###.##"} as String {format: "##,###.00"},
       "quantity": $.quantity
    }
 })

Error Message:

Unable to coerce `` as Number using `##,###.##` as format.

NOTE: if put the data in the position "price "= something the the issue are solve in the first row, but i need use the function try or what do you recomend, i cant validate all the elements in csv because this is a demo the complete file has a many columns... if you would has another coment to better my code i'd apreciated.

Expected Result: (I don't know if this is possible)

[
 {
  "data": [
   {
    "number":"1234557",
    "date":"2022-08-04",
    "items":[
     {
        "upc":"4015",
        "price":"3.00",
        "quantity":"1"
     }
    ]
   }
  ]
  },
  {
  "Error":[
     {
        "number":"1234556",
        "message":"Unable to coerce `` as Number using `##,###.##` as format."
     }
    ]
  }
]

best regards!!

2 Answers

If you are looking to resolve the value of price if price is null/empty value in the input csv and get rid of the error(which is happening because it cannot format Null values to Number) , try adding default in case of empty/null values and formatting to String only when value exists, like below:

%dw 2.0
output application/json
---
payload groupBy ($.number) pluck $ map ( () -> {
   "number": $[0].number,
   "date": $[0].date,
   "items": $ map {
       "upc": $.upc,
       "price": ($.price as String {format: "##,###.00"}) default $.price,
       "quantity": $.quantity
    }
 })

Note:

  1. For price, you don't need to convert to Number at all if you want your output as formatted string ultimately.

Hi The closer I got from what you asked for was

%dw 2.0
output application/json
import * from dw::Runtime

fun safeMap<T, R>(items: Array<T>, callback: (item:T) -> R ): Array<R | {message: String}> = 
    items map ((item) -> try(() -> callback(item)) match {
        case is {success: false} ->  {message: $.error.message as String}
        case is {success: true, result: R} -> $.result
    })
---
payload 
    groupBy ($.number) 
    pluck $ 
    safeMap ((item) -> {
                "number": item[0].number,
                "date": item[0].date,
                "items": item safeMap {
                    "upc": $.upc,
                    "price": $.price as Number {format: "##,###.##"} as String {format: "##,###.00"},
                    "quantity": $.quantity
                }
    })

This uses a combination of map and try function.

And it outputs

[
  {
    "number": "1234556",
    "date": "2022-08-04",
    "items": [
      {
        "message": "Unable to coerce `` as Number using `##,###.##` as format."
      },
      {
        "upc": "4019",
        "price": "2.00",
        "quantity": "1"
      },
      {
        "upc": "4016",
        "price": "3.00",
        "quantity": "1"
      }
    ]
  },
  {
    "number": "1234557",
    "date": "2022-08-04",
    "items": [
      {
        "upc": "4015",
        "price": "3.00",
        "quantity": "1"
      }
    ]
  }
]
Related