gpt4 book ai didi

mule - JSON对象中的关键存在(Mule 4)

转载 作者:行者123 更新时间:2023-12-04 16:26:49 26 4
gpt4 key购买 nike

我需要检查 JSON 对象中是否存在特定关键字,并在此基础上创建一个字符串。我的示例 dataweave 2.0 代码如下:

%dw 2.0
output application/json
var arr = {
"ID": "100",
"ProdId": "Prod",
"ProdName": "ABC"
}
---
if (arr pluck $$ joinBy ';' contains "ProdId") == true
then (String: "Product") else
if (arr pluck $$ joinBy ';' contains "EmpId") == true
then (String: "Employee") else null

根据变量的值,我需要有不同的字符串值。上面需要什么改变才能工作?

最佳答案

可以利用字段存在操作符?:

%dw 2.0
output application/json
var o = {
"ID": "100",
"ProdId": "Prod",
"ProdName": "ABC"
}
---
if (o.ProdId?) (String: "Product") else
if (o.EmpId?) (String: "Employee") else null

您也可以使用 match 运算符解决模式匹配问题:

%dw 2.0
output application/json
var o = {
"ID": "100",
"ProdId": "Prod",
"ProdName": "ABC"
}
---
"ProdId" match {
case "ProdId" -> "String": "Product"
case "EmpId" -> "String": "Employee"
else -> null
}

这是文档:

您还需要注意 String 是 DW 中的一种类型,您必须将其括在引号中("')用作字段名。

关于mule - JSON对象中的关键存在(Mule 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62327032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com