如何在dataweave中子串
How to substring in dataweave
我在输入中得到一个字段 AnnualRevenue,我必须寻找条件,如果该字段中有一个小数,我想将它代入一个整数,但如果它不是小数,它不做任何变换就通过了。谁能告诉我如何在 mulesoft 的 dataweave 中实现这一点。
除了 subtring,您还可以通过创建一个全局 MEL 函数来使用 MEL/java 的整数转换,然后在您的数据编织表达式中使用它。
<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def removeDecimal(value) {
return (long) value
}
</global-functions>
</expression-language>
</configuration>
然后在你的数据编织表达式中:
%dw 1.0
%output application/json
---
{
"result": removeDecimal(-21.83)
}
希望对您有所帮助。
干杯
另一种选择,您可以使用正则表达式:
%dw 1.0
%output application/java
---
{
result: "123.45" replace /(\.[0-9]*)/ with ""
}
input:
<employees>
<employee>
<name>salumiah-syed</name>
</employee>
</employees>
%dw 1.0
%output application/xml
---
result: (payload.employees.employee.name splitBy "-") [0]
或者
%dw 1.0
%output application/java
---
{
value: "123.45",
result: ("123.45" as :number) as :string {format: "#"}
}
参考:https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#coerce-to-string
我在输入中得到一个字段 AnnualRevenue,我必须寻找条件,如果该字段中有一个小数,我想将它代入一个整数,但如果它不是小数,它不做任何变换就通过了。谁能告诉我如何在 mulesoft 的 dataweave 中实现这一点。
除了 subtring,您还可以通过创建一个全局 MEL 函数来使用 MEL/java 的整数转换,然后在您的数据编织表达式中使用它。
<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def removeDecimal(value) {
return (long) value
}
</global-functions>
</expression-language>
</configuration>
然后在你的数据编织表达式中:
%dw 1.0
%output application/json
---
{
"result": removeDecimal(-21.83)
}
希望对您有所帮助。
干杯
另一种选择,您可以使用正则表达式:
%dw 1.0
%output application/java
---
{
result: "123.45" replace /(\.[0-9]*)/ with ""
}
input:
<employees>
<employee>
<name>salumiah-syed</name>
</employee>
</employees>
%dw 1.0
%output application/xml
---
result: (payload.employees.employee.name splitBy "-") [0]
或者
%dw 1.0
%output application/java
---
{
value: "123.45",
result: ("123.45" as :number) as :string {format: "#"}
}
参考:https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#coerce-to-string