尝试使用dataweave仅打印对象中的数字/Id,有人可以看一下吗?

问题描述 投票:0回答:2

输入:

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

这是我的输入,我只想打印名称中的数字/ID

我的预期输出应该是

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

有人给我这样做的逻辑会对你很有帮助。谢谢你

dataweave mulesoft mule4 anypoint-studio anypoint-platform
2个回答
1
投票

这将是实际使用正则表达式的少数情况之一。因为函数 scan() 返回一个数组数组,所以我使用 flatten() 和索引选择器 (

[]
) 来返回实际匹配的子字符串,该子字符串将是数字序列。由于每个字符串中只有一个数字序列,因此它将返回实际数字。

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

输出:

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

0
投票

只需像下面这样完成(使用 https://docs.mulesoft.com/dataweave/latest/dw-strings-functions-isnumeric 函数):

%dw 2.0
output application/json
import isNumeric from dw::core::Strings
---
payload map ($.name filter isNumeric($))
© www.soinside.com 2019 - 2024. All rights reserved.