[英]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.