I am trying to extract category-id when primary = true by grouping of product-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 [] ) ,
}))
I tried with multiple combination and filters. Filter generally works with xml attributes, but here giving exception
cast to com.mulesoft.weave.model.structure.ObjectSeq (java.lang.ClassCastException). Message payload is of type: ReceiverFileInputStream
Sample input -
<?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>
Any suggestions here?
I tried another set of transformation just for demo, not working -
%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")]
})}
Try out this expression. Can you elaborate your expected output?
using (c=payload.catalog.*category-assignment default [])
(c[?(($.primary == "true") and ($.@mode != "delete"))] groupBy $.@product-id map {
field3:$.@category-id,
primary-flag:$.primary
})
Updated dataweave as per your comments:
%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)
Add global configuration element as shown below:
<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>
Try this expression, it will produce the CSV as per your stated requirement(s).
%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 $
)
it produces the output as:
product-id,cat-id,primary,field3
D711069,DANIEL_4160||4160||DANIEL_4160,true,DANIEL_4160
input used:
<?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>
HTH
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.