Mulesoft - Introduction to DataWeave & Transform Message

Mulesoft: Introduction to DataWeave & Transform message Episode #2

Mulesoft

Hey Trailblazers, 🙂

Welcome back to Making an Integration Ranger Episode #2. Please find the agenda that we are going to learn today in Episode #2.

Access Event Properties

In some scenarios, we wanted to access the properties of the Event like the Query Parameters, URI parameters, Method, or other attributes. For example, get the values from the URL parameters in the request. You need to traverse the message like below

#[ message.attributes.queryParams.firstName as String ]

The Transform Message Component

The Transform (or Transform Message) component converts input data to a new output structure or format.

Drag & Drop view :

transform message component concept design center b3dc0

Preview

JSON file used for Demo

[
  {
    "Name": "Week 8 - Trigger Demo",
    "Phone": null,
    "Industry": null,
    "Rating": "Warm",
    "Description": "Before Update Account Name has been changed from Week 7 to Week 8 - Trigger Demo"
  },
  {
    "Name": "GenePoint",
    "Phone": "(650) 867-3450",
    "Industry": "Biotechnology",
    "Rating": "Cold",
    "Description": "Genomics company engaged in mapping and sequencing of the human genome and developing gene-based drugs"
  },
  {
    "Name": "United Oil & Gas, UK",
    "Phone": "+44 191 4956203",
    "Industry": "Energy",
    "Rating": null,
    "Description": null
  },
  {
    "Name": "United Oil & Gas, Singapore",
    "Phone": "(650) 450-8810",
    "Industry": "Energy",
    "Rating": null,
    "Description": null
  },
  {
    "Name": "Edge Communications",
    "Phone": "(512) 757-6000",
    "Industry": "Electronics",
    "Rating": "Hot",
    "Description": "Edge, founded in 1998, is a start-up based in Austin, TX. The company designs and manufactures a device to convert music from one digital format to another. Edge sells its product through retailers and its own website."
  },
  {
    "Name": "Week 8",
    "Phone": "(336) 222-7000",
    "Industry": "Banking",
    "Rating": "Warm",
    "Description": "Before Update Account Name has been changed from Burlington Textiles Corp of America to Week 8"
  },
  {
    "Name": "Pyramid Construction Inc.",
    "Phone": "(014) 427-4427",
    "Industry": "Construction",
    "Rating": null,
    "Description": null
  },
  {
    "Name": "Dickenson plc",
    "Phone": "(785) 241-6200",
    "Industry": "Consulting",
    "Rating": null,
    "Description": null
  },
  {
    "Name": "Grand Hotels & Resorts Ltd",
    "Phone": "(312) 596-1000",
    "Industry": "Hospitality",
    "Rating": "Warm",
    "Description": "Chain of hotels and resorts across the US, UK, Eastern Europe, Japan, and SE Asia."
  },
  {
    "Name": "Express Logistics and Transport",
    "Phone": "(503) 421-7800",
    "Industry": "Transportation",
    "Rating": "Cold",
    "Description": "Commerical logistics and transportation company."
  }
]

DataWeave code used in Demo

%dw 2.0
output application/xml

import dw::core::Strings
import camelize, capitalize from dw::core::Strings
import * from dw::core::Strings

var myvar = "amit singh"

var toUpper = (aString) -> 
	if(aString=="Hot")
		"too hot"
	else
		"too Cold"
		
fun capit(rating:String) = 
	if(rating == "Hot")
		"Amit"
	else 
		"Singh"
	
---

accounts : {
	(payload map () ->{
		account @(name : $.Name) : $.Description,
		Phone : $.Phone,
		name : toUpper(myvar),
		name : capit(myvar)
	})
}
/*
accounts_1 : {
		(payload map () ->{
			accounts : {
				account : $
			}	
		})
	} */
	

Complete flow from Day 2

Watch all this in Action:

Sharing is caring. #KeepLearning #KeepBlazing #RoadToIntegrationRanger #Mulesoft

References