如何在 Mulesoft 中比较 Xml 中的 json 对象值
How to Compare json Object value in Xml within Mulesoft
我的Json有效载荷是
[
{
"Customer Contacted": "Rupesh",
"Inspected": "Mahesh",
"Lost": "Fire",
"Job Start Date": "",
"Work Complete": "",
"Billing Complete": ""
}
]
我的XML是
%dw 2.0
output application/xml
---
{
root: {
CustomerContacted: if(payload."Customer Contacted"=="Rupesh") "Hari"
else null,
Inspected: if(payload."Inspected"=="Mahesh")"vamsi"
else null,
Lost: payload.Lost,
JobStartDate: payload."Job Start Date",
WorkComplete: payload."Work Complete",
BillingComplete: payload."Billing Complete"
}
}
如果负载中的值与 If Condition 中提供的值匹配,则应更改该值。
转换后我低于输出。
<?xml version='1.0' encoding='UTF-8'?>
<root>
<CustomerContacted/>
<Inspected/>
<Lost>Fire</Lost>
<JobStartDate></JobStartDate>
<WorkComplete></WorkComplete>
<BillingComplete></BillingComplete>
</root>
我在已联系客户和已检查字段中得到空值,但我希望值“Rupesh”和“Mahesh”为“Hari”和“Vamsi”
请告诉我 XML 代码中需要更改的内容。
payload是一个数组,要将元素匹配到字符串,需要定义索引。例如,在您的 payload 上,Customer Contacted
是对象的一部分,您的 payload 类型是一个数组。
语法应该是这样的:
if(payload."Customer Contacted"[0] == "Rupesh") "Hari"
else null
示例:
%dw 2.0
output application/xml
---
{
root: {
CustomerContacted: if(payload."Customer Contacted"[0] == "Rupesh") "Hari"
else null,
Inspected: if(payload."Inspected"[0]=="Mahesh")"vamsi"
else null,
Lost: payload.Lost[0],
JobStartDate: payload."Job Start Date"[0],
WorkComplete: payload."Work Complete"[0],
BillingComplete: payload."Billing Complete"[0]
}
}
由于您的输入是一个数组,您需要对其进行迭代以获取值或使用数组符号 [0] 对索引进行硬编码。我建议使用 map
函数,因为如果你得到一个大小大于 1 的输入数组,硬编码索引是不可扩展的。类似下面的东西应该适用于这种情况:
%dw 2.0
output application/xml
---
root: {
subroot: payload map {
CustomerContacted: if($."Customer Contacted"=="Rupesh") "Hari" else null,
Inspected: if($."Inspected"=="Mahesh") "vamsi" else null,
Lost: $.Lost,
JobStartDate: $."Job Start Date",
WorkComplete: $."Work Complete",
BillingComplete: $."Billing Complete"
}
}
注意:如果输入数组中有多个项目,则需要输出 subroot
字段,否则会出现错误:Trying to output second root
因为无效的 xml 结构会导致.
我的Json有效载荷是
[
{
"Customer Contacted": "Rupesh",
"Inspected": "Mahesh",
"Lost": "Fire",
"Job Start Date": "",
"Work Complete": "",
"Billing Complete": ""
}
]
我的XML是
%dw 2.0
output application/xml
---
{
root: {
CustomerContacted: if(payload."Customer Contacted"=="Rupesh") "Hari"
else null,
Inspected: if(payload."Inspected"=="Mahesh")"vamsi"
else null,
Lost: payload.Lost,
JobStartDate: payload."Job Start Date",
WorkComplete: payload."Work Complete",
BillingComplete: payload."Billing Complete"
}
}
如果负载中的值与 If Condition 中提供的值匹配,则应更改该值。 转换后我低于输出。
<?xml version='1.0' encoding='UTF-8'?>
<root>
<CustomerContacted/>
<Inspected/>
<Lost>Fire</Lost>
<JobStartDate></JobStartDate>
<WorkComplete></WorkComplete>
<BillingComplete></BillingComplete>
</root>
我在已联系客户和已检查字段中得到空值,但我希望值“Rupesh”和“Mahesh”为“Hari”和“Vamsi”
请告诉我 XML 代码中需要更改的内容。
payload是一个数组,要将元素匹配到字符串,需要定义索引。例如,在您的 payload 上,Customer Contacted
是对象的一部分,您的 payload 类型是一个数组。
语法应该是这样的:
if(payload."Customer Contacted"[0] == "Rupesh") "Hari"
else null
示例:
%dw 2.0
output application/xml
---
{
root: {
CustomerContacted: if(payload."Customer Contacted"[0] == "Rupesh") "Hari"
else null,
Inspected: if(payload."Inspected"[0]=="Mahesh")"vamsi"
else null,
Lost: payload.Lost[0],
JobStartDate: payload."Job Start Date"[0],
WorkComplete: payload."Work Complete"[0],
BillingComplete: payload."Billing Complete"[0]
}
}
由于您的输入是一个数组,您需要对其进行迭代以获取值或使用数组符号 [0] 对索引进行硬编码。我建议使用 map
函数,因为如果你得到一个大小大于 1 的输入数组,硬编码索引是不可扩展的。类似下面的东西应该适用于这种情况:
%dw 2.0
output application/xml
---
root: {
subroot: payload map {
CustomerContacted: if($."Customer Contacted"=="Rupesh") "Hari" else null,
Inspected: if($."Inspected"=="Mahesh") "vamsi" else null,
Lost: $.Lost,
JobStartDate: $."Job Start Date",
WorkComplete: $."Work Complete",
BillingComplete: $."Billing Complete"
}
}
注意:如果输入数组中有多个项目,则需要输出 subroot
字段,否则会出现错误:Trying to output second root
因为无效的 xml 结构会导致.