Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

5/22/19

Module 5: Operating upon


Arrays and Objects

Goal

namespace URL count or sum up

<sales:order xmlns:ord="http://crm.acme.com" totalQty=6 total=240.00>

<sales:item price=100.00 qty=2>Base Station</sales:item>


namespace prefix
<sales:item price=10.00 qty=4>Sensor</sales:item>

</sales:users>

attributes

All contents © M uleSoft Inc. 2

1
5/22/19

At the end of this module, you should be able to

• Conditionally test, count, and sum up elements of an array using the


dw::core::Arrays module
• Divide an array into smaller child arrays using the divide function
• Extract out key names, key namespaces, key attributes, and values
of an object by using the dw::core::Objects module
• Select key names, key namespaces, key attributes, and values of
descendants of an object by using selectors

All contents © M uleSoft Inc. 3

Testing and operating on array


elements by using the
dw::core::Arrays module

2
5/22/19

The dw::core::Arrays module contains functions to


test and operate on array elements

• The module is imported into the DataWeave header or expression


• Each function takes two parameters
– An input array
– A conditional expression

All contents © M uleSoft Inc. 5

Operating upon arrays by using the


dw::core::Arrays module

Function Description Example Result


some Tests if any array elements match a [1,3,5] some ( ($ mod 2) == 0 ) false
condition expression
every Tests if every array element [1,3,5] every ( ($ mod 2) == 1 ) true
matches a condition

countBy Counts the number of array ["AAB", "ABA", "BAA"] countBy 2


elements matching a condition ( $ contains "AA" )

sumBy Applies an expression to each [{qty:10}, {qty:15}, {qty:20}] 4.5


array element, then adds up the sumBy
( $.qty / 10 )
results

divideBy Divides (partitions) an array into 1 to 5 divide by 2 [


as many equal sized sub-arrays [1,2], [3,4], [5]
]
as possible, then appends the
remaining elements as a
sub-array
All contents © M uleSoft Inc. 8

3
5/22/19

Walkthrough 5-1: Operate upon array elements by


using the dw::core::Arrays module

• Test if some or all array elements match a condition


• Count how many array elements match a condition
• Apply a function to each element of an array then sum the results
together
• Divide an array into smaller sub-arrays

All contents © M uleSoft Inc. 9

Extracting parts of an object

4
5/22/19

Use the DataWeave core Objects module to extract


parts of an object

• The nameSet function gives an array of an object's key names


• The keySet function gives an array of an objet's key names, key
namespaces, and key attributes
• The valueSet function gives an array of an object's values
• The entrySet function gives a list of key/value pairs
– Each element of the list breaks out the key name, value, and key attributes of
each element of an object

All contents © M uleSoft Inc. 11

Example: Demonstrating the difference between the


keySet and nameSet functions
{
output application/json "keySet": {
"keys": [
import dw::core::Objects "user1",
"user2"
var myVar = read(' ],
<users xmlns="http://test.com"> "nameSpaces": [
<user1 name="Mariano" lastName="Achaval">MA</user1> "http://test.com",
<user2 name="Stacey" lastName="Duke">SD</user2> "http://test.com"
</users> ],
', 'application/xml') "attributes": [
{
--- "name": "Mariano",
{ "lastName": "Achaval"
keySet: { },
keys: Objects::keySet(myVar.users) map ( (user) -> user ), {
nameSpaces: Objects::keySet(myVar.users) map ( (user) -> user.# ), "name": "Stacey",
attributes: Objects::keySet(myVar.users) map ( (user) -> user.@ ), "lastName": "Duke"
}, }
nameSet: { ]
keys: Objects::nameSet(myVar.users) map ( (user) -> user ), },
nameSpaces: Objects::nameSet(myVar.users) map ( (user) -> user.# ), "nameSet": {
attributes: Objects::nameSet(myVar.users) map ( (user) -> user.@ ), "keys": ["user1","user2"],
} "nameSpaces": [null,null],
} "attributes": [null,null]
}
All contents © M uleSoft Inc. 12
}

5
5/22/19

Example: Extracting the values of an Object

output application/json {
"valueSet": {
import dw::core::Objects "keys": [
"MA",
var myVar = read(' "SD"
<users xmlns="http://test.com"> ],
<user1 name="Mariano" lastName="Achaval">MA</user1> "nameSpaces": [
<user2 name="Stacey" lastName="Duke">SD</user2> "http://test.com",
</users> "http://test.com"
', 'application/xml') ],
"attributes": [
--- {
{ "name": "Mariano",
valueSet: { "lastName": "Achaval"
keys: Objects::valueSet(myVar.users) map ( (user) -> user ), },
nameSpaces: Objects::valueSet(myVar.users) map ( (user) -> user.# ), {
attributes: Objects::valueSet(myVar.users) map ( (user) -> user.@ ) "name": "Stacey",
}, "lastName": "Duke"
}
]
}

All contents © M uleSoft Inc. 13

Example: Extracting the entrySet of an Object


{
output application/json "entrySet": {
"entrySetKeys": [
import dw::core::Objects
"user1",
"user2"
var myVar = read('
],
<users xmlns="http://test.com"> "entrySetValues": [
<user1 name="Mariano" lastName="Achaval">MA</user1> "MA",
<user2 name="Stacey" lastName="Duke">SD</user2>
"SD"
</users> ],
', 'application/xml')
"entrySetAttributes": [
{
--- "name": "Mariano",
{
"lastName": "Achaval"
entrySet: { },
entrySet: Objects::entrySet(myVar.users) map ( (user) -> user),
{
entrySetKeys: Objects::entrySet(myVar.users) "name": "Stacey",
map ( (user) -> user.key), "lastName": "Duke"
}
entrySetValues: Objects::entrySet(myVar.users) ]
map ( (user) -> user.value), }
}
entrySetAttributes: Objects::entrySet(myVar.users)
map ( (user) -> user.attributes)
}
} All contents © M uleSoft Inc. 14

6
5/22/19

Merging two objects together

• The mergeWith function replaces key/value pairs in one object with


matching key/values in another object

Header / Input Body expression Output


%dw 2.0 {
output application/json one:"ONE",
one: "One2",
two:"TWO" {
} "two": "TWO",
"one": "Number1"
dw::core::Objects::mergeWith }

{one:"Number1}

All contents © M uleSoft Inc. 15

Walkthrough 5-2: Manipulate parts of objects by


using the dw::core::Objects module

• Separate out the key, value, and attributes of each element of an


object by using dw::core::Objects module functions
• Merge one object with another object by using the mergeWith
function

All contents © M uleSoft Inc. 16

7
5/22/19

Selecting keys and values from


an object

Selecting descendent data in a complex schema

Selector Type Syntax Return Type


Single Value selector .<key-name> Value of a key:value pair
Array with values of key:value
Multi Value selector .*<key-name>
pairs
Array with values of key:value
Descendants selector ..<key-name>
pairs
Key-Value Pair selector .&<key-name> Object with key:value pairs

Indexed selector [<index>] Value at selected array index


Array with values from
Range selector [<index> to <index>]
selected indexes
XML Attribute selector .@<key-name> Value for selected attribute
Object with attributes as
All XML Attributes .@
key:value pairs
String of the namespace used
Namespace selector .#
at the current key
All contents © M uleSoft Inc. 18

8
5/22/19

Selecting from objects

• DataWeave provides various selectors to extract keys or key/values from an object


or an array of objects
• For an array of objects, the selectors can also be used on each object inside a map
operator
Input Transform Output
var payload = %dw 2.0
[ output application/xml // XML output causes an error
---
{ users: payload map (object, index) -> { Cannot coerce an array to an
"firstname":"Annie", fname: object.firstname, object
"lastname":"Point" lname: object.lastname
}, }
{
"firstname":"Ed", output application/xml
---
"lastname":"Mule" users: <users>
} {( <fname>Annie</fname>
] payload map (object, index) -> { <lname>Point</lname>
fname: object.firstname, <fname>Ed</fname>
lname: object.lastname <lname>Mule</lname>
} </users>
)}
All contents © M uleSoft Inc. 19

Using selectors in DataWeave expressions

Selector type Description Example


All descendants selector An array of every descendant. Selector is payload..
called recursively on each outer value
Single Value selector Value of the first outer key matching the key payload.name
name attributes.queryParams

Indexed selector Select one value from the results of the payload.name[0]
.keyname selector

Multi Value selector Array of values corresponding to outer level payload.*name


keys matching the supplied key name

Descendant selector Array of values of the first matching key of payload..zip


each recursive selector call

Descendants selector Array of values of all matching keys of all payload..*zip


descendant key:value pairs

All contents © M uleSoft Inc. 20

9
5/22/19

Selecting outer elements of an object

Input Transform Output


var payload = //select the first match {
{ payload.findFlightResponse "return": {
"findFlightResponse": { "airlineName": "United"
"return": { },
//select the first of all matches
"airlineName": "United" "return": {
}, "airlineName": "Delta"
payload.*findFlightResponse[0]
"return": { }
"airlineName": "Delta" }
}
//select all matches [
} payload.*findFlightResponse {
} "return": {
"airlineName": "United"
},
"return": {
"airlineName": "Delta"
}
}
]

All contents © M uleSoft Inc. 21

Selecting outer elements of an object

Input Transform Output


var payload = //select the first match
{ payload.findFlightResponse.return
"findFlightResponse": { {
"return": { "airlineName": "United",
//select the first of all matches
"airlineName": "United" }
},
payload.findFlightResponse.*return[0]
"return": {
"airlineName": "Delta"
} //select all matches
[
} payload.findFlightResponse.*return
{
} "airlineName": "United"
},
{
"airlineName": "Delta"
}
]

All contents © M uleSoft Inc. 23

10
5/22/19

Selecting all descendants of an object

• The .. selector returns an array of every descendent value


Input Transform Output
var payload = payload.. [
{ {
"findFlightResponse": { "return": {
"return": { "airlineName": "United",
},
"airlineName": "United"
"return": {
}, "airlineName": "Delta"
"return": {
}
"airlineName": "Delta" },
} {
} "airlineName": "United"
} },
"United"
{
"airlineName": "Delta"
},
"Delta"
]

All contents © M uleSoft Inc. 24

Selecting descendant object values matching a key


name

• Select a specific key name from the array of descendants


Input Transform Output
var payload = // Every descendant value [
{ // for the key named return {
"findFlightResponse": { payload..*return "airlineName": "United"
"return": { },
"airlineName": "United" (payload..).*return {
}, "airlineName": "Delta"
"return": { }
"airlineName": "Delta" ]
}
// First descendant value [ { "airlineName": "United" } ]
} // for the key named return
} payload..return
(payload..).return

(payload..).*return[0] { "airlineName": "United" }

All contents © M uleSoft Inc. 25

11
5/22/19

Selecting descendant literal values matching a key


name

Input Transform Output


var payload = payload..price
{
"findFlightResponse": {
"return": {
"airlineName": "United",
"code":"UA231",
"price":310.10
},
"return": { [ 310.10, 220.00]
payload..*price
"airlineName": "Delta",
"code":"DL40",
"price":220.00
}
}
}

All contents © M uleSoft Inc. 26

Selecting descendant key/values matching a key


name with object values

• Select a specific key name from the array of descendants


Input Transform Output
var payload = // Every descendant key/value [
{ // for the key named return {
return: {
"findFlightResponse": { payload..&return "airlineName": "United"
"return": { },
"airlineName": "United" (payload..).&return return: {
}, "airlineName": "Delta"
"return": { }
}
"airlineName": "Delta" ]
}
// flatten the array of objects {
} return: {
} {( payload..&return )}
"airlineName": "United"
},
// First descendant key/value return: {
// for the key named return ( "airlineName": "Delta"
}
payload..&return[0] }

All contents © M uleSoft Inc. 27

12
5/22/19

Selecting descendant key/values matching a key


name with literal values

• Select a specific key name from the array of descendants


Input Transform Output
var payload = // Every descendant key/value
[
{ // for the key named return
{
"findFlightResponse": { payload..&airlineName "airlineName": "United"
"return": { },
"airlineName": "United" (payload..).&airlineName {
}, "airlineName": "Delta"
}
"return": {
]
"airlineName": "Delta"
} {
// Flatten the array of objects
} "airlineName": "United",
{( payload..&airlineName )}
}
"airlineName": "Delta"
}
// Pick the first object
payload..&airlineName[0] { "airlineName": "United" }

All contents © M uleSoft Inc. 28

Selecting descendant key/values matching a key


name for a literal value

Input Transform Output


var payload = // Every descendant key/value [
{ // for the key named price {"price":310.10},
"findFlightResponse": { payload..&price {"price":220.00}
"return": { ]
(payload..).&price
"airlineName": "United",
"code":"AA231",
"price":310.10
// First descendant key/value {
},
// for the key named price "price":310.10,
"return": {
payload..&price[0] "price":220.00
"airlineName": "Delta",
}
"code":"UA40",
"price":220.00
// flatten the array of objects
}
{( payload..&price )}
}
}

All contents © M uleSoft Inc. 29

13
5/22/19

Selecting descendant namespaces from an object

Input Transform Output


output application/dw payload..airlineName[0].# %dw 2.0
ns fl http://flights.com
ns fl http://flights.com ns air http://airlines.com
ns air http://airlines.com ---
{
var payload = uri: "http://airlines.com",
{
prefix: "air"
findFlightResponse: {
fl#return: { }as Namespace
air#airlineName: "United", payload..airlineName[0].#.prefix %dw 2.0
code: "AA231", ns fl http://flights.com
price: 310.10 ns air http://airlines.com
}, ---
fl#return: { "air"
air#airlineName: "Delta",
code: "UA40",
price: 220.00
}
}
}

All contents © M uleSoft Inc. 30

Selecting descendant namespaces from an object

Input Transform Output


output application/dw payload..airlineName[0].# "http://airlines.com"

ns fl http://flights.com
ns air http://airlines.com
payload..airlineName map $.#.prefix [
var payload = "air",
{ "air"
findFlightResponse: { ]
fl#return: {
air#airlineName: "United", payload..return.# null
code: "AA231",
price: 310.10
}, payload..return map $.#.uri [
fl#return: { "http://flights.com"
air#airlineName: "Delta", ]
code: "UA40",
payload..*return map $.#.uri [
price: 220.00
"http://flights.com",
}
} "http://flights.com"
} ]

All contents © M uleSoft Inc. 31

14
5/22/19

Selecting descendant attributes from an object

Input Transform Output


output application/json payload..*return.@ [
{"airlineName:"united"},
ns fl http://flights.com payload..&return.return.@ {"airlineName:"delta"}
ns air http://airlines.com ]
payload..airlineName map [
var payload ={
$.#.prefix "air",
findFlightResponse: {
"air"
fl#return @(airline:"united"):{
]
air#airlineName: "United",
"code":"AA231", payload..return.# null
"price":310.10
},
fl#return @(airline:"delta"): { payload..return map $.#.uri [
air#airlineName: "Delta", "http://flights.com"
"code":"UA40", ]
"price":220.00 payload..*return map $.#.uri [
} "http://flights.com",
} "http://flights.com"
} ]
All contents © M uleSoft Inc. 32

Walkthrough 5-3: Select keys, values, attributes,


and namespace from descendant objects

• Select one or multiple values or key/values from descendant objects


that match a key name
• Select key namespaces or key attributes from descendant objects

All contents © M uleSoft Inc. 33

15
5/22/19

Summary

Summary

• Construct new arrays and objects with the +, ++, -, and – operators
• Test and operate on array elements using the dw::core::Arrays
module
• Operate on elements of an object using the dw::core::Objects
module
• Create new objects with object constructor curly braces {} and
evaluation parentheses ()
• Use selectors to extract descendant values, key/values, namespaces,
and attributes

All contents © M uleSoft Inc. 35

16

You might also like