简体   繁体   中英

How to distinctBy after splitBy MuleSoft Dataweave

I have splitBy one of my field and would like to distinct it by showing only 1 record per duplicate. eg, if have 5 id = 'abc' then it will only show 1 id = 'abc' on the result.

Based on the below sample output, how do I distinct it to show only 1 ABC123 instead of 2 ABC123?

The distinctBy that I have put completely remove the field FruitTypeCode on my output, may I know the reason of causing it?

Sample Input

<GetListOfCategoriesDetailResponse xmlns="" xmlns:s="">
    <GetListOfCategoriesDetailResult xmlns:a="" xmlns:i="">
        <a:CategoryDetail>
            <a:Category>ABC123 Kook</a:Category>
            <a:CategoryClass>Apple</a:CategoryClass>
            <a:FruitTypeGrouping/>
        </a:CategoryDetail>
        <a:CategoryDetail>
            <a:Category>ABC123 Loop</a:Category>
            <a:CategoryClass>Apple</a:CategoryClass>
            <a:FruitTypeGrouping/>
        </a:CategoryDetail>
        <a:CategoryDetail>
            <a:Category>BCD344 78JL</a:Category>
            <a:CategoryClass>Apple</a:CategoryClass>
            <a:FruitTypeGrouping/>
        </a:CategoryDetail>
    </GetListOfCategoriesDetailResult>
</GetListOfCategoriesDetailResponse>

Current Output

 <ns0:FoodProducts>
     <ns0:FoodProduct>
         <ns0:FruitTypes>
             <ns0:FruitType FruitTypeCode="ABC123" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text>This is sweet</ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
             <ns0:FruitType FruitTypeCode="ABC123" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text>This is not sweet</ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
             <ns0:FruitType FruitTypeCode="BCD344" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text></ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
     </ns0:FoodProduct>
 </ns0:FoodProducts>

Expected Output

 <ns0:FoodProducts>
     <ns0:FoodProduct>
         <ns0:FruitTypes>
             <ns0:FruitType FruitTypeCode="ABC123" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text>This is sweet</ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
             <ns0:FruitType FruitTypeCode="BCD344" FruitTypeName="Apple" FruitTypeGroup="">
                 <ns0:Descriptions>
                     <ns0:Description>
                         <ns0:Text></ns0:Text>
                     </ns0:Description>
                 </ns0:Descriptions>
             </ns0:FruitType>
     </ns0:FoodProduct>
 </ns0:FoodProducts>

My thus far dataweave code

%output application/xml
 %namespace ns0 
 %namespace ns01 
 %namespace ns1 
 ---
 {
     ns0#FoodProductRS: {
         ns0#FoodProducts: {
             ns0#FoodProduct: {
                 ns0#FruitTypes: {         
  ((payload.ns01#GetListOfCategoriesDetailResponse.ns01#GetListOfCategoriesDetailResult.*ns1#CategoryDetail filter $.ns1#CategoryClass == "Apple" map ((categoryDetail , indexOfCategoryDetail) -> {
                         ns0#FruitType @(FruitTypeCode: (categoryDetail.ns1#Category splitBy " ")[0], FruitTypeName: categoryDetail.ns1#CategoryClass , FruitTypeGroup: categoryDetail.ns1#FruitTypeGrouping): {
                             ns0#Descriptions: {
                                 ns0#Description: {
                                     ns0#Text: categoryDetail.ns1#LongDescription
                                 }
                             }

                         }
                     })) distinctBy $.ns0#FruitType.@FruitTypeCode)
                 }
             }
         }
     }
 } 

I've tried your script in mule 3.9.2 and it should work, but sadly you are facing an issue (it is going to be present on 3.9.3). In order to workaround it you should make distinctBy first.

%output application/xml
     %namespace ns0 a
     %namespace ns01 a
     %namespace ns1 a
     ---
     {
         ns0#FoodProductRS: {
             ns0#FoodProducts: {
                 ns0#FoodProduct: {
                     ns0#FruitTypes: {         
                        (
                            (payload.ns01#GetListOfCategoriesDetailResponse.ns01#GetListOfCategoriesDetailResult.*ns1#CategoryDetail 
                                filter $.ns1#CategoryClass == "Apple" 
                                distinctBy ($.ns1#Category splitBy " ")[0]
                                map ((categoryDetail , indexOfCategoryDetail) -> {
                                     ns0#FruitType @(FruitTypeCode: (categoryDetail.ns1#Category splitBy " ")[0], FruitTypeName: categoryDetail.ns1#CategoryClass , FruitTypeGroup: categoryDetail.ns1#FruitTypeGrouping): {
                                         ns0#Descriptions: {
                                             ns0#Description: {
                                                 ns0#Text: categoryDetail.ns1#LongDescription
                                             }
                                         }

                                     }
                                })
                             ) 
                        )
                     }
                 }

What mule version are you using?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM