SlideShare a Scribd company logo
Francesco Ganora
DataWeave
A functional
data transformation language
from MuleSoft
The data mapping challenge
JSON
XML
CSV
Fixed Width
POJO
JSON
XML
CSV
Fixed Width
POJO
Structural Transformation
Value Transformation
Conditional mapping
Filtering
Grouping
Best practice: always define the mapping in terms of the desired target data structure
The old programmatic approach
❖ Map the target message from the source message
programmatically (e.g., via a script or Java method)
❖ Sequence of procedural steps that incrementally build the
target message from the source message
❖ Typical example: loop on elements of a source sequence
and for each element instantiate a target sub-structure, then
attach it to the overall target structure
❖ This approach is neither concise nor expressive; if
implemented incorrectly, it is also inefficient
The templating approach
❖ Template engines can be used as
data mapping engines:
❖ We define the target structure
(template)
❖ We define how each part of the
template is generated dynamically
from source data
❖ The template consists of a semi-
literal expression with
placeholders e.g. $() in the this
example
❖ More constructs are necessary to
instantiate repetitive structures
(looping), for conditional
mapping, etc.
{“user”:
{“id”: “$(sourceData.userID)”,
“firstName”: “$(sourceData.givenName)”,
“lastName”: “$(sourceData.lastName)”,
“contacts”: {
“phone”: “$(sourceData.phoneNumber)”,
“email”: “$(sourceData.emailAddress)”
}}
<?xml version="1.0">
<user>
<id> $(sourceData.userID) </id>
<firstName> $(sourceData.givenName) </firstName>,
<lastName> $(sourceData.lastName) </lastName>
<contacts>
<phone> $(sourceData.phoneNumber) </phone>
<email> $(sourceData.emailAddress) </email>
</contacts>
</user>
JSON
XML
Issues with standard templating
❖ Template depends on the concrete syntax of the target message (separate
templates for XML, JSON etc.)
❖ Placeholder syntax depends on the type of source message (e.g., XPath for
XML, JSONPath for JSON, non-standard syntax for other media types)
❖ Placeholder syntax may clash with target message syntax (cannot use for
example <> as placeholder markers with XML)
❖ Looping constructs of traditional template engines mix engine syntax with
generated content (“PHP-like”)
❖ XSLT is a very powerful templating and transformation language, but it
does have drawbacks (verbose XML syntax, cannot operate on non-tree-
structured source message that cannot be rendered into XML, etc.)
DataWeave (DW)
❖ Data mapping and
transformation tool from
MuleSoft
❖ Tightly integrated with
AnyPoint Studio IDE
❖ Non-procedural expression
language
❖ Applies functional
programming constructs
(lambdas)
❖ Uses internal, canonical data
format (application/dw)
Canonical data representation
1. DW parses the source message into application/dw canonical format using supplied metadata
/ DataSense capability
2. A DW expression is used to transform the source message (result still in canonical application/
dw format)
3. DW renders the canonical target message into the target MIME type specified as a “header”
to the DW expression (e.g. %output application/json)
This decouples the transformation from the concrete syntax of source and target messages!
Source
message
<source MIME type>
parser renderer
Source
message
(canonical)
Target
message
(canonical)
Target
message
DW
expression
<target MIME type>application/dw application/dw
The DW canonical format
❖ Only 3 kinds of data in SW:
• Simple (String, Number,
Boolean, Date types)
• Array
• Objects (key:value pairs)
❖ The canonical application/dw format
is shown in a JSON-like concrete
syntax in Anypoint Studio
❖ Parsing and rendering between
application/json and application/dw
is straightforward
[
{
"order_nr": "DO1234",
"order_date": "2016-03-12T13:30:23+8.00",
sku: "1233244",
"sku_description": "Product A",
qty: "20"
},
{
"order_nr": "DO1234",
"order_date": "2016-03-12T13:30:23+8.00",
sku: "1233255",
"sku_description": "Product B",
qty: "50"
}
]
XML Parsing
❖ repeated XML elements —> repeated object keys
❖ XML attributes —> special @() object
CSV parsing
❖ Array of records (lines)
❖ Record (line) —> array
element of type Object
❖ Field in record: object
field (key is taken from
CSV header line or
configured metadata)
❖ Reader configuration to
set field separator, etc.
DW transform structure
%dw 1.0
%input payload application/csv
%output application/json
%type sapDate = :string { format: “YYYYMMDD” }
%var unitOfMeasure = 'EA'
%var doubleNumber = (nr) -> [nr * 2.0]
%namespace xsi http://www.w3.org/2001/XMLSchema-instance
%function fname(name) {firstName: upper name}
——-
order: {
ID: payload.orderID ++ " dated " ++ payload.orderDate,
nrLines: (sizeOf payload.orderItems) + 1,
totalOrderAmount: payload.*orderItems reduce
$$ + (($.orderQuantity as :number) * ($.unitPrice as :number))
}
}
Optional header contains:
• transformation directives
• reusable declarations
Body contains the DW
transformation expression
Case study: introduction
Transforming a list of order items into a corresponding list of delivery routes.
The source payload is unsorted list of items in CSV format:
OrderId;OrderDate;CustomerId;DeliveryDate;City;ProductId;Quantity
000001;2016-09-14;Customer1;2016-09-20;London;ProductA;120
000001;2016-09-14;Customer1;2016-09-20;London;ProductB;88
000002;2016-09-15;Customer2;2016-09-20;Paris;ProductC;60
000002;2016-09-15;Customer2;2016-09-20;Paris;ProductA;100
000002;2016-09-15;Customer2;2016-09-20;Paris;ProductD;15
000003;2016-09-15;Customer3;2016-09-23;Berlin;ProductB;14
000003;2016-09-15;Customer3;2016-09-23;Berlin;ProductD;30
000004;2016-09-15;Customer4;2016-09-20;London;ProductC;14
000004;2016-09-15;Customer4;2016-09-20;London;ProductE;30
000005;2016-09-16;Customer4;2016-09-20;London;ProductB;20
000006;2016-09-16;Customer2;2016-09-22;Paris;ProductD;7
000006;2016-09-16;Customer2;2016-09-22;Paris;ProductE;30
000007;2016-09-16;Customer5;2016-09-22;Berlin;ProductB;12
The target structure (described in the following slide) is a multi-level JSON structure.
This case study focuses on the structural transformation capabilities of DW, but DW offers a
wide range of value and formatting capabilities, conditional mapping, and much more!
Case study: target format
[
{
city: "<City>",
deliveryDate: "<DeliveryDate>",
stops: [
{
customer: "<CustomerId>",
orderitems: [
{
ordernr: "<OrderId>",
orderdate: "<OrderDate>",
product: "<ProductId>",
qty: "<Quantity>"
}
]
}
]
}
]
JSON document with
sequence of delivery
routes by delivery date
and city:
❖ Sort CSV order lines by
city and delivery date
❖ Within each delivery
date and city, group
order lines by customer
❖ Render the structure as
JSON
By city / delivery date
By customer
By order item
Case study: step 1
Source message parsed as application/dw:
The DW expression payload evaluates the entire message payload (see earlier slide “CSV parsing)”
NOTE: the DW transformer Preview functionality in MuleSoft Anypoint Studio maps the sample
source in realtime as you type the transformation!
Case study: step 2
Sorting and grouping by combination of city and delivery date:
A composite key is used for sorting and grouping via the string concatenation operator (++) .
The groupBy operator creates an object with the group values as keys.
Case study: step 3
Iterating over the group values (city/delivery date combination) to
generate the 1st level of the target structure:
The pluck operator maps an object into an array. $$ is the key in the current iteration, $ is the
value.
City and delivery date are mapped from the composite key by String manipulation.
Case study: step 4
Within each route group, group by customer and generate 2nd (inner) level of target
structure:
In the inner pluck the context for $ and $$ changes (e.g., $$ is now the CustomerID key).
Case study: (final) step 5
Within each customer group, generate the 3rd (innermost) level of the target
structure via the map operator:
Also get the JSON rending by changing the %output directive.
Thanks!
This is just a “taste” of the innovative DataWeave
transformation language.
Find out more at:
https://docs.mulesoft.com/mule-user-guide/v/3.8/
dataweave

More Related Content

Viewers also liked (15)

PPTX
Overview of XSL, XPath and XSL-FO
Suite Solutions
 
PPTX
Mulesoft API
Kleverton Fortunato
 
PPTX
Deploying mule applications
Bhargav Ranjit
 
PPTX
Operators in mule dataweave
Ramakrishna kapa
 
PPTX
Mule esb data weave multi input data
AnilKumar Etagowni
 
PPTX
Why Integrate using an API? | MuleSoft
Bui Kiet
 
PPTX
Mule data weave_6
kunal vishe
 
PPTX
Mule esb :Data Weave
AnilKumar Etagowni
 
PPTX
SOAP To REST API Proxy
Vince Soliza
 
PPTX
MuleSoft London Community - API Marketing, Culture Change and Tooling
Pace Integration
 
PPTX
ADP: Driving Faster Customer Onboarding with MuleSoft - Michael Bevilacqua, V...
MuleSoft
 
PPTX
How Cisco is Leveraging MuleSoft to Drive Continuous Innovation​ at Enterpris...
MuleSoft
 
PDF
The Emerging Integration Reference Architecture | MuleSoft
MuleSoft
 
PPTX
Microservices Best Practices
MuleSoft
 
PDF
IoT architecture
Sumit Sharma
 
Overview of XSL, XPath and XSL-FO
Suite Solutions
 
Mulesoft API
Kleverton Fortunato
 
Deploying mule applications
Bhargav Ranjit
 
Operators in mule dataweave
Ramakrishna kapa
 
Mule esb data weave multi input data
AnilKumar Etagowni
 
Why Integrate using an API? | MuleSoft
Bui Kiet
 
Mule data weave_6
kunal vishe
 
Mule esb :Data Weave
AnilKumar Etagowni
 
SOAP To REST API Proxy
Vince Soliza
 
MuleSoft London Community - API Marketing, Culture Change and Tooling
Pace Integration
 
ADP: Driving Faster Customer Onboarding with MuleSoft - Michael Bevilacqua, V...
MuleSoft
 
How Cisco is Leveraging MuleSoft to Drive Continuous Innovation​ at Enterpris...
MuleSoft
 
The Emerging Integration Reference Architecture | MuleSoft
MuleSoft
 
Microservices Best Practices
MuleSoft
 
IoT architecture
Sumit Sharma
 

Similar to MuleSoft DataWeave data transformation language (20)

PPT
Data weave reference documentation
D.Rajesh Kumar
 
PPTX
Data weave documentation
Sindhu VL
 
PPTX
Data weave documentation
Khadhar Koneti
 
PPT
Accessing loosely structured data from F# and C#
Tomas Petricek
 
PDF
MuleSoft Nashik Virtual Meetup#3 - Deep Dive Into DataWeave and its Module
Jitendra Bafna
 
PPTX
Machine Learning - Dataset Preparation
Andrew Ferlitsch
 
PPT
Javascript Templating
bcruhl
 
PPTX
Data weave component
Sindhu VL
 
PPT
Syntactic Mediation in Grid and Web Service Architectures
Martin Szomszor
 
PPT
Concepts In Object Oriented Programming Languages
ppd1961
 
PPT
Hadoop_Pennonsoft
PennonSoft
 
PPT
EclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
Dave Steinberg
 
PPT
Hadoop - Introduction to mapreduce
Vibrant Technologies & Computers
 
PPTX
Mule data weave_2
kunal vishe
 
PDF
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
CHOOSE
 
PPTX
Mule with data weave
Son Nguyen
 
PDF
Writing DSL with Applicative Functors
David Galichet
 
PPTX
Data weave (MuleSoft)
Nandu List5
 
Data weave reference documentation
D.Rajesh Kumar
 
Data weave documentation
Sindhu VL
 
Data weave documentation
Khadhar Koneti
 
Accessing loosely structured data from F# and C#
Tomas Petricek
 
MuleSoft Nashik Virtual Meetup#3 - Deep Dive Into DataWeave and its Module
Jitendra Bafna
 
Machine Learning - Dataset Preparation
Andrew Ferlitsch
 
Javascript Templating
bcruhl
 
Data weave component
Sindhu VL
 
Syntactic Mediation in Grid and Web Service Architectures
Martin Szomszor
 
Concepts In Object Oriented Programming Languages
ppd1961
 
Hadoop_Pennonsoft
PennonSoft
 
EclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
Dave Steinberg
 
Hadoop - Introduction to mapreduce
Vibrant Technologies & Computers
 
Mule data weave_2
kunal vishe
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
CHOOSE
 
Mule with data weave
Son Nguyen
 
Writing DSL with Applicative Functors
David Galichet
 
Data weave (MuleSoft)
Nandu List5
 
Ad

Recently uploaded (20)

PDF
Modern Decentralized Application Architectures.pdf
Kalema Edgar
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PPTX
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PPTX
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
PDF
Linux schedulers for fun and profit with SchedKit
Alessio Biancalana
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
[GDGoC FPTU] Spring 2025 Summary Slidess
minhtrietgect
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
Modern Decentralized Application Architectures.pdf
Kalema Edgar
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
Linux schedulers for fun and profit with SchedKit
Alessio Biancalana
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
[GDGoC FPTU] Spring 2025 Summary Slidess
minhtrietgect
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
Ad

MuleSoft DataWeave data transformation language

  • 1. Francesco Ganora DataWeave A functional data transformation language from MuleSoft
  • 2. The data mapping challenge JSON XML CSV Fixed Width POJO JSON XML CSV Fixed Width POJO Structural Transformation Value Transformation Conditional mapping Filtering Grouping Best practice: always define the mapping in terms of the desired target data structure
  • 3. The old programmatic approach ❖ Map the target message from the source message programmatically (e.g., via a script or Java method) ❖ Sequence of procedural steps that incrementally build the target message from the source message ❖ Typical example: loop on elements of a source sequence and for each element instantiate a target sub-structure, then attach it to the overall target structure ❖ This approach is neither concise nor expressive; if implemented incorrectly, it is also inefficient
  • 4. The templating approach ❖ Template engines can be used as data mapping engines: ❖ We define the target structure (template) ❖ We define how each part of the template is generated dynamically from source data ❖ The template consists of a semi- literal expression with placeholders e.g. $() in the this example ❖ More constructs are necessary to instantiate repetitive structures (looping), for conditional mapping, etc. {“user”: {“id”: “$(sourceData.userID)”, “firstName”: “$(sourceData.givenName)”, “lastName”: “$(sourceData.lastName)”, “contacts”: { “phone”: “$(sourceData.phoneNumber)”, “email”: “$(sourceData.emailAddress)” }} <?xml version="1.0"> <user> <id> $(sourceData.userID) </id> <firstName> $(sourceData.givenName) </firstName>, <lastName> $(sourceData.lastName) </lastName> <contacts> <phone> $(sourceData.phoneNumber) </phone> <email> $(sourceData.emailAddress) </email> </contacts> </user> JSON XML
  • 5. Issues with standard templating ❖ Template depends on the concrete syntax of the target message (separate templates for XML, JSON etc.) ❖ Placeholder syntax depends on the type of source message (e.g., XPath for XML, JSONPath for JSON, non-standard syntax for other media types) ❖ Placeholder syntax may clash with target message syntax (cannot use for example <> as placeholder markers with XML) ❖ Looping constructs of traditional template engines mix engine syntax with generated content (“PHP-like”) ❖ XSLT is a very powerful templating and transformation language, but it does have drawbacks (verbose XML syntax, cannot operate on non-tree- structured source message that cannot be rendered into XML, etc.)
  • 6. DataWeave (DW) ❖ Data mapping and transformation tool from MuleSoft ❖ Tightly integrated with AnyPoint Studio IDE ❖ Non-procedural expression language ❖ Applies functional programming constructs (lambdas) ❖ Uses internal, canonical data format (application/dw)
  • 7. Canonical data representation 1. DW parses the source message into application/dw canonical format using supplied metadata / DataSense capability 2. A DW expression is used to transform the source message (result still in canonical application/ dw format) 3. DW renders the canonical target message into the target MIME type specified as a “header” to the DW expression (e.g. %output application/json) This decouples the transformation from the concrete syntax of source and target messages! Source message <source MIME type> parser renderer Source message (canonical) Target message (canonical) Target message DW expression <target MIME type>application/dw application/dw
  • 8. The DW canonical format ❖ Only 3 kinds of data in SW: • Simple (String, Number, Boolean, Date types) • Array • Objects (key:value pairs) ❖ The canonical application/dw format is shown in a JSON-like concrete syntax in Anypoint Studio ❖ Parsing and rendering between application/json and application/dw is straightforward [ { "order_nr": "DO1234", "order_date": "2016-03-12T13:30:23+8.00", sku: "1233244", "sku_description": "Product A", qty: "20" }, { "order_nr": "DO1234", "order_date": "2016-03-12T13:30:23+8.00", sku: "1233255", "sku_description": "Product B", qty: "50" } ]
  • 9. XML Parsing ❖ repeated XML elements —> repeated object keys ❖ XML attributes —> special @() object
  • 10. CSV parsing ❖ Array of records (lines) ❖ Record (line) —> array element of type Object ❖ Field in record: object field (key is taken from CSV header line or configured metadata) ❖ Reader configuration to set field separator, etc.
  • 11. DW transform structure %dw 1.0 %input payload application/csv %output application/json %type sapDate = :string { format: “YYYYMMDD” } %var unitOfMeasure = 'EA' %var doubleNumber = (nr) -> [nr * 2.0] %namespace xsi http://www.w3.org/2001/XMLSchema-instance %function fname(name) {firstName: upper name} ——- order: { ID: payload.orderID ++ " dated " ++ payload.orderDate, nrLines: (sizeOf payload.orderItems) + 1, totalOrderAmount: payload.*orderItems reduce $$ + (($.orderQuantity as :number) * ($.unitPrice as :number)) } } Optional header contains: • transformation directives • reusable declarations Body contains the DW transformation expression
  • 12. Case study: introduction Transforming a list of order items into a corresponding list of delivery routes. The source payload is unsorted list of items in CSV format: OrderId;OrderDate;CustomerId;DeliveryDate;City;ProductId;Quantity 000001;2016-09-14;Customer1;2016-09-20;London;ProductA;120 000001;2016-09-14;Customer1;2016-09-20;London;ProductB;88 000002;2016-09-15;Customer2;2016-09-20;Paris;ProductC;60 000002;2016-09-15;Customer2;2016-09-20;Paris;ProductA;100 000002;2016-09-15;Customer2;2016-09-20;Paris;ProductD;15 000003;2016-09-15;Customer3;2016-09-23;Berlin;ProductB;14 000003;2016-09-15;Customer3;2016-09-23;Berlin;ProductD;30 000004;2016-09-15;Customer4;2016-09-20;London;ProductC;14 000004;2016-09-15;Customer4;2016-09-20;London;ProductE;30 000005;2016-09-16;Customer4;2016-09-20;London;ProductB;20 000006;2016-09-16;Customer2;2016-09-22;Paris;ProductD;7 000006;2016-09-16;Customer2;2016-09-22;Paris;ProductE;30 000007;2016-09-16;Customer5;2016-09-22;Berlin;ProductB;12 The target structure (described in the following slide) is a multi-level JSON structure. This case study focuses on the structural transformation capabilities of DW, but DW offers a wide range of value and formatting capabilities, conditional mapping, and much more!
  • 13. Case study: target format [ { city: "<City>", deliveryDate: "<DeliveryDate>", stops: [ { customer: "<CustomerId>", orderitems: [ { ordernr: "<OrderId>", orderdate: "<OrderDate>", product: "<ProductId>", qty: "<Quantity>" } ] } ] } ] JSON document with sequence of delivery routes by delivery date and city: ❖ Sort CSV order lines by city and delivery date ❖ Within each delivery date and city, group order lines by customer ❖ Render the structure as JSON By city / delivery date By customer By order item
  • 14. Case study: step 1 Source message parsed as application/dw: The DW expression payload evaluates the entire message payload (see earlier slide “CSV parsing)” NOTE: the DW transformer Preview functionality in MuleSoft Anypoint Studio maps the sample source in realtime as you type the transformation!
  • 15. Case study: step 2 Sorting and grouping by combination of city and delivery date: A composite key is used for sorting and grouping via the string concatenation operator (++) . The groupBy operator creates an object with the group values as keys.
  • 16. Case study: step 3 Iterating over the group values (city/delivery date combination) to generate the 1st level of the target structure: The pluck operator maps an object into an array. $$ is the key in the current iteration, $ is the value. City and delivery date are mapped from the composite key by String manipulation.
  • 17. Case study: step 4 Within each route group, group by customer and generate 2nd (inner) level of target structure: In the inner pluck the context for $ and $$ changes (e.g., $$ is now the CustomerID key).
  • 18. Case study: (final) step 5 Within each customer group, generate the 3rd (innermost) level of the target structure via the map operator: Also get the JSON rending by changing the %output directive.
  • 19. Thanks! This is just a “taste” of the innovative DataWeave transformation language. Find out more at: https://docs.mulesoft.com/mule-user-guide/v/3.8/ dataweave