Saturday, April 30, 2016

MuleSoft Dataweave Tutorial Part IV: Using mapObject, pluck, function

Introduction

DataWeave comes with map, mapOjbect, pluck, and other build in functions. The map function is mostly used in the message transformation. There are a lot of other functions provided by the Dataweave component, such as split, trim, uppercase, lower case, etc.

This tutorial is an example on how to use map, mapObject, plunk, and joinBy functions to achieve a fairly complicated message transformation.

Use Cases

Input Data


   
   
      
         venkat
         api-user-jwilson
         4JuGSrVb1E7k8a1datKuGV0fqma58c2B
         VC84632147254611111111
         5000
         
            John Smith
            12 Main St
            Raheja
            30301
            Denver
            CO
            USA
            6099026676
            gg@gmail.com
            10.121.123.112
         
         
            John Smith
            12 Main St
            SR Nagar
            30301
            Denver
            CO
            USA
         
         
            111
            false
            V12345
            MOTO
            true
            true
            H
            3000
            1000
            false
            
               
                  1
                  1
                  T1234522
                  100
                  localsales
                  A10004
                  AA01
                  100
                  400
                  300
               
               
                  02
                  1
                  120
                  120
                  30
                  t-shirt
                  each
                  300
                  0001
               
                
                  001
                  1
                  120
                  120
                  30
                  t-shirt
                  each
                  300
                  0001
                                  
            
            
               
            
         
      
   


Output

{
  "result": "(productcode=02;quantity=1;price=120;tax=120;discount=30;productdescription=t-shirt;unitofmeasure=each;taxamount=300;taxidentifier=0001)(productcode=001;quantity=1;price=120;tax=120;discount=30;productdescription=t-shirt;unitofmeasure=each;taxamount=300;taxidentifier=0001)"
}

We need to create key-value pairs for level-e-field. Each key-value pair is separated by semi-colon, and each object is demarked by round braket. (key1=value1;key2=value2;...;keyN=valueN)().

The Solution

%dw 1.0
%output application/json
%function enrich(val) '(' ++ val ++ ')' 
---
result: payload.Envelope.Body.preauth.optional-info.level-2-3-fields.*level-3-field
map ((value,index) -> (
      value mapObject ((oValue, oKey) -> { 
          '$oKey': oKey ++ '=' ++ oValue        
      }) pluck $ joinBy ';'
)) 
map ((val, key) -> enrich(val)) joinBy ''

The first code line 5 is easy to understand. We need to access the xml object at level-3-file. The only trick we need to know is that we need to add a star *. As we have multiple level-3-field object, we need to use map. map function is just a look. It takes two parameters. The first is value of the object, and the second is the index. If you have an array or list, you can think the first is the value of the array and the second is the index, which starts with 0 as java array.

In our code, we use twice joinBy function. The first joins the array with ';' semi-colon. The second join the level-3-field objects together.

pluck is another function which create an array of keys or values. The value is $, and the keys are $$.

2 comments:

  1. windows 10 product key and activation , windows 7 ult keys retail , windows 10 activation phone number , key window 7 , windows 10 product key boot camp , free windows anytime upgrade key for windows 7 professional 32 bit , windows 10 product key clean install , buy cheap windows 8.1 pro key , luidRb

    buy office 2016 product key

    windows server 2012 r2 free

    rosetta stone french key sale

    windows 10 product serial number offer

    windows 7 product serial number

    ReplyDelete
  2. I really appreciate information shared above. It’s of great help.

    Mulesoft online training hyderabad

    ReplyDelete

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...