[英]Mule Dataweave Object to Array Error
我正在尝试通过对product-id进行分组来提取primary = true时的category-id
%input payload application/xml
%output application/java
---
using (c=payload.catalog.*category-assignment default [])
((c default []) filter ($.@mode != "delete") groupBy $.@product-id map ({
(($ default []) filter ($.primary == 'true') map ({
field3:$.@category-id
}) when $.primary != null otherwise field3:"" ),
cat-id: $.@category-id joinBy "||" ,
primary-flag:$.primary
// (($ default []) filter ($.primary matches /true/) map ({
// //ur code here when equals to shipping charges
// field3:$.@category-id
// }) when $.primary != null otherwise [] ) ,
}))
我尝试了多种组合和过滤器。 过滤器通常适用于xml属性,但此处给出了例外
cast to com.mulesoft.weave.model.structure.ObjectSeq (java.lang.ClassCastException). Message payload is of type: ReceiverFileInputStream
样本输入-
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://www.example.com/xml/impex/catalog/2006-10-31">
<category-assignment product-id="D711069" category-id="4160">
<primary>true</primary>
</category-assignment>
<category-assignment product-id="D711069" category-id="DANIEL_4160"/>
</catalog>
有什么建议吗?
我尝试了另一组仅用于演示的转换,但无法正常工作-
%input payload application/xml
%output application/xml
---
Test:{((payload.catalog.*category-assignment default []) groupBy $.@product-id pluck {
product-id:$$,
cat-id: $.@category-id joinBy "||" ,
primary-flag:$[0].primary,
field3:$.@category-id[?($.primary == "true")]
})}
试试这个表达式。 您能否详细说明预期的输出?
using (c=payload.catalog.*category-assignment default [])
(c[?(($.primary == "true") and ($.@mode != "delete"))] groupBy $.@product-id map {
field3:$.@category-id,
primary-flag:$.primary
})
根据您的评论更新了dataweave:
%dw 1.0
%output application/csv
---
payload.catalog.*category-assignment[?(($.primary == "true") and ($.@mode != "delete"))] groupBy $.@product-id map {
productid:$.@product-id[0],
categoryid: arrayToString($.@category-id, sizeOf $.@category-id)
添加全局配置元素,如下所示:
<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def arrayToString(arrayObj,length){
String r=null;
if(arrayObj==null){
return "null";
}else if(arrayObj==""){
return "Empty";
}else{
for (String s:arrayObj) {
if(r != null)
r = r + '||' + s;
else {
r = s;
}
}
return r;
}
}
</global-functions>
</expression-language>
</configuration>
尝试使用此表达式,它将根据您指定的要求生成CSV。
%dw 1.0
%output application/csv header=true
---
using (ca = payload.catalog.*category-assignment)
(
payload.catalog map (catVal, idx) -> ({
(
ca groupBy $.@product-id pluck {
product-id:$$,
cat-id: $.@category-id joinBy "||"
,primary-flag:$.primary[0] default "false"
,(field3:$.@category-id[0]) when ($.primary[0] contains "true")
}
)
}) distinctBy $
)
它产生的输出为:
product-id,cat-id,primary,field3
D711069,DANIEL_4160||4160||DANIEL_4160,true,DANIEL_4160
使用的输入:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<category-assignment product-id="D711069" category-id="DANIEL_4160"/>
<category-assignment product-id="D711069" category-id="4160">
<primary>true</primary>
</category-assignment>
<category-assignment product-id="D711069" category-id="DANIEL_4160"/>
</catalog>
高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.