Thursday, April 13, 2017

DataWeave 101!

DataWeave! DataWeave! DataWeave! It's fun to do dataweaving....

How to create a lookup function in DataWeave?

%var gendertable = { "Male": "M", "Female": "F", "Unknown": "U"}
- - -
...
genderCode: gendertable[payload.inputGender]
...

How to display a data with a valid xml descriptor?

Input:
<root>
    <Address>
        <country>USA</country>
        <type>
            <reference descriptor="Home">nvm</reference>
        </type>
    </Address>
    <Address>
        <country>PH</country>
        <type>
            <reference descriptor="Work">nvm</reference>
        </type>
    </Address>
</root>

Transform:

Option 1 =

%dw 1.0
%output application/json
---
payload.root.*Address map (
      { homeCountry: $.country } when $.type.reference.@descriptor == "Home" otherwise { homeCountry: "" }
)

Option 2 =

(
  payload.root.*Address 
    filter $.type.reference.@descriptor == 'Home' 
    map {
      homeCountry: $.country
    }
)[0]

Output:

{
    "homeCountry" : "USA"
}


How to use the value to be the key?

Input:

<ns6:ProductListResponse xmlns:ns5="http://www.twcable.com/common/types/v1x0"
  xmlns:ns6="http://www.twcable.com/soa/types/csg/v2x21">
         <ns6:Business>
            <ns6:BusinessUnit>815020000070</ns6:BusinessUnit>
         </ns6:Business>
         <ns6:ProductList>
            <ns6:Count>2</ns6:Count>
            <ns6:Product>
               <ns6:Code>$F</ns6:Code>
               <ns6:AvailableCatalogIdentifier>60060840415</ns6:AvailableCatalogIdentifier>
               <ns6:CatalogIdentifier>600606044</ns6:CatalogIdentifier>
               <ns6:ChildCount>2</ns6:ChildCount>
               <ns6:ParameterCount>1</ns6:ParameterCount>
               <ns6:Parameter>
                  <ns6:Identifier>6006060093417</ns6:Identifier>
                  <ns6:Name>FROM_REFERENCE_NAME</ns6:Name>
                  <ns6:DomainDefinition>N</ns6:DomainDefinition>
               </ns6:Parameter>
            </ns6:Product>
            <ns6:Product>
               <ns6:Code>$H</ns6:Code>
               <ns6:AvailableCatalogIdentifier>60060840416</ns6:AvailableCatalogIdentifier>
               <ns6:CatalogIdentifier>600606045</ns6:CatalogIdentifier>
               <ns6:ParameterCount>2</ns6:ParameterCount>
               <ns6:Parameter>
                  <ns6:Identifier>6006060093453</ns6:Identifier>
                  <ns6:Name>RSTRCT CALLER ID</ns6:Name>
               </ns6:Parameter>
               <ns6:Parameter>
                  <ns6:Identifier>6006060093446</ns6:Identifier>
                  <ns6:Name>BLOCK 900/976</ns6:Name>
                </ns6:Parameter>
           </ns6:Product>
       </ns6:ProductList>
</ns6:ProductListResponse>

Transform:

%dw 1.0
%output application/java
%var products = payload.ProductListResponse.ProductList.*Product
---
{
  Status:"Success",
  Response: {(
    products map {
      ($.CatalogIdentifier): {($.*Parameter map {
          ($.Name): $.Identifier
        }
      )}
    }
  )}
}

Output:

Status:"Success",
Response:{
  600606044 : {
    "FROM_REFERENCE_NAME","6006060093417"
  },
  600606045 : {
    "RSTRCT CALLER ID":"6006060093453",
    "BLOCK 900/976":"6006060093446"
  }
}



How can I filter the objects where "UniqueId": "0"

{
    Header: {
        MsgId: flowVars.msgCId,
        Key: flowVars.key
    },
    Data: {
        Addresses: 
payload.ns0#GetCustomerDataResponse.ns0#OCust.*ns0#OCadRecord[?($.ns0#OCAddrId != "0")]
 map ((oCadRecord , indexOfOCadRecord) -> {
            Type: oCadRecord.ns0#OCAddrUsageCd,
            DescriptionOfOther: oCadRecord.ns0#OCAddrDesc,
            CountryCd: oCadRecord.ns0#OCCntryCd,
            Line1: oCadRecord.ns0#OCStAddrTx,
            Line2: oCadRecord.ns0#OCStAddr2Tx,
            Line3: oCadRecord.ns0#OCStAddr3Tx,
            City: oCadRecord.ns0#OCAddrCityNm,
            State: oCadRecord.ns0#OCAddrStateCd,
            Zip: oCadRecord.ns0#OCPostalCd,
            Zip4: oCadRecord.ns0#OCUsZipSuffCd,
            UniqueId: oCadRecord.ns0#OCAddrId
        })
    }
}




6 comments:

James G said...

Wow, I couldn't find anywhere but here. Thanks for sharing about dataweave MuleSoft. It's hard to find an example about it. MuleSoft is great and simple but their documents is kind of confusing sometimes. Your example is simple and practical. This is what we need. My team has been looking this kind of example. Thanks again.

Unknown said...

Ray, been trying to reach you to discuss a new Mulesoft developer role that I have but you changed your number since we last spoke. Please contact me at nrc@bgcs.com if you are open to a new role. Thanks! - Nick

On the supplements said...

Thanks a lot for being our mentor on this niche. I enjoyed your current article very much and most of all cherished how you really handled the aspect I considered to be controversial. You are always quite kind to readers much like me and let me in my everyday living. Thank you.

Kayal said...

Great content! The information is very helpful for me and Thanks for your sharing. Please continue your good job!
Corporate Training in Chennai
Corporate Training institute in Chennai
Excel Training in Chennai
Social Media Marketing Courses in Chennai
Pega Training in Chennai
Embedded System Course Chennai
Linux Training in Chennai
Primavera Training in Chennai
Corporate Training in Chennai
Corporate Training institute in Chennai


info said...

You are so thrilling! I do no longer consider I've definitely look at via a few thing like that before. So wonderful to find out some other person with a few unique mind on this issue. Seriously.. net info Many thank you for beginning this up. This website is some component this is required on the net, a person with a chunk of originality!

kosmik said...

Tank you for sharing this blog some of them are use useful those who searching training institutes details.click it Now