Trying to print only the numbers/Id's in the object using dataweave, can someone take a look?

117 views Asked by At

Input:

[
  {
    "name": "TOOL: 5237"
  },
  {
    "name": "DMP: 213213 RENTAL"
  },
  {
    "name": "SMG: 23434-A"
  }
]

This is my Input , I want to only print the numbers/ID's in the name

my Expected output should be

[
  
    "5237"
  ,
    "213213"
  ,
    "23434"
  
]

It would be helpful you someone gives me the logic to do it. Thank you

2

There are 2 answers

0
StackOverflowed On

Simply done like below (with https://docs.mulesoft.com/dataweave/latest/dw-strings-functions-isnumeric function):

%dw 2.0
output application/json
import isNumeric from dw::core::Strings
---
payload map ($.name filter isNumeric($))
0
aled On

This would be one of the few cases to actually use a regular expression. Because the function scan() returns an array of arrays I used flatten() and the indexed selector ([]) to return the actual matching sub string that will be a sequence of digits. Since there is one and only one sequence of digits in each strings it will return the actual number.

%dw 2.0
output application/json  
---
payload map (flatten(($.name scan (/([\d]+)/)))[0])

Output:

[
  "5237",
  "213213",
  "23434"
]