Saturday, April 16, 2016

MuleSoft Dataweave Tutorial: Part II - Functions

User Defined Functions

Here is a example from Mule document:
%dw 1.0
%output application/json
%function toUser(user) { firstName: user.givenName, lastName: user.sn }
---
{
  "user" : toUser({ givenName : "Annie", sn : "Point" })
}
Output from the result:
{
  "user": {
    "firstName": "Annie",
    "lastName": "Point"
  }
}

Mule document did not provide much explanation. What does this function do?

 %function toUser(user) { firstName: user.givenName, lastName: user.sn }
First, we can see it declares the user object, which has {givenName, sn}. When we use the function in the body, we parse an object of user in the form of:
 toUser({ givenName : "Annie", sn : "Point" }) 
Where
 { givenName : "Annie", sn : "Point" } 
is an user object. This tell us how you can initialize an object and pass to a function.

Using Build-in Function: GroupBy

If we want to group object by attribute, we can groupBy and pluck together. Here is the simple Dataweave code:

%dw 1.0
%output application/json
---
payload groupBy $.state

Here is the input data:
[
 {
  "name": "John Smith",
  "street": "1234 JS Ln",
  "city": "Allen",
  "state": "TX",
  "zip": "75123" 
 },
 {
  "name": "Framk Chambers",
  "street": "1234  Colleage Rd",
  "city": "Stillwater",
  "state": "OK",
  "zip": "73001" 
 },
 {
  "name": "Robert Goll",
  "street": "1234 Bush Dr",
  "city": "Plano",
  "state": "TX",
  "zip": "75127" 
 },
  {
  "name": "Billie Chambers",
  "street": "1234 College Rd",
  "city": "Stillwater",
  "state": "OK",
  "zip": "73001" 
 }
]
And here is the output:
[
  {
    "name": "John Smith"
  },
  {
    "name": "Robert Goll"
  },
  {
    "name": "Framk Chambers"
  },
  {
    "name": "Billie Chambers"
  }
]

10 comments:

  1. awesome post presented by you..your writing style is fabulous and keep update with your blogs.

    Mulesoft online training bangalore

    ReplyDelete
  2. the blog showa detailed mulesoft datawave tutorial it is useful for basic Developers of mulesoft kindly update more Info about
    mulesoft. for updated information go with
    mulesoft Online Training

    For more online Trainings Go with

    tableau online training


    ServiceNow Online Training

    Python Online Training

    ReplyDelete
  3. This was an excellent blog,your writing style is fabulous and keep update with your blogs.
    Big Data Hadoop online Course


    ReplyDelete
  4. It is nice blog Thank you porovide importent information and i am searching for same information to save my time thank you given mulesoft training. get more...
    ios Online Training

    ReplyDelete

  5. the blog is good and Interactive it is about MuleSoft Dataweave Tutorial: Part II - Functions it is useful for students and Mulesoft Developers for more updates on Mulesoft mulesoft Online course

    ReplyDelete
  6. This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me.mulesoft training in bangalore





    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-...