Contact Free trial Login

Extract Data

DataWeave can select data from objects and arrays, variables that store that data, and the output of DataWeave functions when that output is an array or object. You can use selectors in Mule modules, connectors, and components that accept DataWeave expressions.

A selector operates within a context, which can be a reference to the variable that stores the data, an object literal, an array literal, or the invocation of a DataWeave function. For examples, see Single-Value Selector (.keyName).

Using Selectors on Arrays and Objects

When referencing an array or object, you use selector expressions to extract specific values from within a data structure.

Note that Studio and Design Center require DataWeave expressions to reside between square brackets that are preceded by a hash (or pound) symbol, for example:

#[input_data.input_key]

Valid keys must start with a letter that is optionally followed by any combination of numbers, letters, or underscores (_).

  • For valid keys in your data, quotation marks around the key and selector are optional. All of these examples work: {"name" : "somebody"}.name, {"name" : "somebody"}."name", {name : "somebody"}.name, {name : "somebody"}."name".

  • For invalid keys, you must surround the key and selector in single-quotes or double-quotes. For example, for an invalid key like some-name, { "some-name" : "somebody" }."some-name" works, but { some-name : "somebody" }.some-name produces the error Invalid input '-'. Both the key and the selector require the quotation marks.

Data to extract Examples

Single Value (.)

. selector acts on arrays and objects.

  • Selecting a name value from an object:

    { "id" : "1234", "name" : "me" }.name

    Returns "me" if the output directive is application/json.

  • Selecting a name value from an array:

    [ { "id" : "1234", "name" : "me" } ].name
    [ { "id" : "1234" }, { "name" : "me" } ].name

    Each example returns [ "me" ] if the output directive is application/json.

  • Selecting the first name value:

    { user : "a", "user" : "b" }.user

    Only returns the first user value, "a" if the output directive is set to application/json.

For more examples, see Single-Value Selector (.keyName).

Multiple Values (*)

* selector acts on arrays and objects.

  • Selecting all user values from an array:

    [ { "user" : "a", "user" : "b" } ].*user

    Returns ["a", "b"] if the output directive is application/json.

  • Selecting all user values from an object:

    { user : "a", "user" : "b" }.*user

    Returns: ["a", "b"] if the output directive is application/json.

  • Selecting all user values from the users key:

    { "users" : [ { "user" : "a" }, { "user" : "b" } ] }.users.*user`

    Returns: [ "a", "b" ] if the output directive is application/json.

For more examples, see Multi-Value Selector (.*).

Key-Value Pair (&)

& selector acts on arrays and objects.

  • Selecting values from the & key:

    { "users" : [ "user" : "ale", "something": "else", "user" : "joe" ] }.users.&user

    Returns [ { "user": "ale" }, { "user": "joe" } ] if the output directive is application/json.

For more examples, see Key-Value Pair Selector (.&keyName).

Descendants (..)

.. selector acts on arrays and objects.

  • Selecting values from the c key:

    { "user" : [{ "a" : "b"}, {"c" : [ {"aa" : "bb"}, { "cc": "dd" } ] } ] , "user" : "d" }..c

    Returns [ [ { "aa": "bb" }, { "cc": "dd" } ] ].

For more examples, see Descendants Selector (..keyName).

Index ([])

[] selector acts on indices of an array.

  • Selecting an index from an array:

    • [1,2,3,4][0] returns [1]

      The index of the first element in an array is always 0.

    • [1,2,3,4][4] returns [3]

Range [index1 to index2]

[index1 to index2] selector produces and array with values from selected indexes.

  • Selecting a range of indices:

    • [0,1,2][0 to 1] returns [ 0, 1 ]

    • [0,1,2][-1 to 0] returns an inverted list: [ 2, 1, 0 ]

For more examples, see Range Selectors

XML attribute .@keyName

.@keyName selector returns the value of the selected attribute.

For an example, see XML Attribute Selector (.@keyName).

Namespace Selector keyName.#

keyName.# selector returns the namespace of the selected XML element.

For an example, see Namespace Selector (#).

Selector Modifiers ? and !

? and ! check for the specified key. ? returns true or false. ! returns an error if the key is not present.

  • { "a": "b" }.a? returns true. { "a": "b" }.c? returns false.

  • { "a": "b" }.a! returns "b". { "a": "b" }.c! produces the error There is no name 'c'. { "a": "b" }.c (without !) returns null.

For more examples, see Selector Modifiers (!, ?).

Filter Selectors keyName[?(booleanExpression)]

keyName[?(booleanExpression)] returns the selected items if the Boolean expression returns true and the specified key is present. It returns null if the expression is false or the key is not present.

Metadata Selector .^someMetadata

Returns the value of specified metadata for a Mule payload, variable, or attribute. The selector can return the value of class (.^class), encoding (.^encoding), MIME type (.^mimeType), media type (.^mediaType), raw (.^raw), and custom (.^myCustomMetadata) metadata. See Metadata Selector (.^someMetadata).

  • Class metadata: .^class returns the class of the Plain Old Java Object (POJO). For example, { "string" : payload.string.^class } might return { "string": "java.lang.String" } if the input payload defines a Java string, such as simplePojo.string = "myString", in a simple POJO, and { "date" : payload.date.^class } might return { "date": "java.util.Date" }. For an example, see Class Metadata Selector (.^class).

  • Encoding metadata: .^encoding returns the encoding of a value. For example, { "myEncoding" : payload.^encoding } might return {"myEncoding": "UTF-8"} for an input POJO. For an example, see Encoding Metadata Selector (.^encoding).

  • Media Type Selector: .^mediaType returns the MIME type of a value that includes parameters, for example, application/json;charset=UTF-16, and the expression in the value of { "myMediaType" : payload.^mediaType } might return "myMediaType": "/; charset=UTF-8" for an input POJO. For an example, see Media Type Metadata Selector (.^mediaType).

  • MIME Type metadata: .^mimeType returns the MIME type (without parameters) of a value, for example, application/json, and { "myMimeType" : payload.^mimeType } might return { "myMediaType": "/" } for an input POJO. For an example, see MIME Type Metadata Selector (.^mimeType).

  • Raw metadata: .^raw returns the underlying data (typically, a binary value) of a plain old Java object (POJO). This selector is sometimes used when calculating an MD5 for hashes when checking for man-in-the-middle attacks. For examples, see Raw Metadata Selector (.^raw).

  • Custom metadata: .^myCustomMetadata returns the value of custom metadata. For examples, see [caret_custom_metadata].

Single-Value Selector (.keyName)

.keyName selectors work over an object or array.

  • Given the object { "aString": "hello", "aNum": 2, "aBool" : false }, a single-value selector can select the value of one of the object’s key-value pairs, for example:

    { "aString": "hello", "aNum": 2, "aBool" : false }.aString

    Returns a string: "hello"

  • Given the array [ "aString": "hello", "aNum": 2, "aBool" : false, "aString" : "world" ], a single-value selector can select values from the array’s key-value pairs, for example:

    [ "aString": "hello", "aNum": 2, "aBool" : false, "aString" : "world" ].aString

    Returns an array: [ "hello", "world" ]

  • Supported variable references are Mule Runtime variables, such as payload and attributes, and DataWeave variables that store arrays or objects. A simple example is payload.someKey where the payload is the object {"someKey" : 1234 }, so the result is 1234.

  • A selector can act on the invocation of a function, such as the DataWeave read function. For example, read('{"A":"B"}','application/json')."A" returns "B".

Single-Value Selector Over an Object

.keyName returns the value of the first matching key. It does not return values of any subsequent, duplicate keys. For example, { "aString": "hello", "aNum": 2, "aBool" : false, "aString" : "world" }.aString returns [ "hello" ], not [ "hello", "world" ].

When DataWeave processes a selector, it sets a new context (or scope) for subsequent selectors, so you can navigate through the complex structures of arrays and objects using chains of selectors. The depth of the selection is limited only by the depth of the current context.

In this example, payload.people.person.address returns the value of the address element.

DataWeave Script:
%dw 2.0
output application/xml
---
{ address: payload.people.person.address }
Input Payload:
{
  "people": {
    "size" : 1,
    "person": {
      "name": "Nial",
      "address": {
        "street": {
          "name": "Italia",
          "number": 2164
        },
        "area": {
          "zone": "San Isidro",
          "name": "Martinez"
        }
      }
    }
  }
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<address>
  <street>
    <name>Italia</name>
    <number>2164</number>
  </street>
  <area>
    <zone>San Isidro</zone>
    <name>Martinez</name>
  </area>
</address>

Single-Value Selector Over an Array

.keyName returns an array with the values of all matching keys in the input array.

When acting on an array, the single-value selector returns an array even if there is only one matching value. For example, ["a":"b"]."a" returns ["b"].

In the following example, the value of the input payload, people, is an array that contains two objects. The selector navigates both objects and returns the values of both street keys.

DataWeave Script:
%dw 2.0
output application/json
---
payload.people.person.address.street
Input Payload
{
  "people": [
    {
      "person": {
        "name": "Nial",
        "address": {
          "street": {
            "name": "Italia",
            "number": 2164
          },
          "area": {
            "zone": "San Isidro",
            "name": "Martinez"
          }
        }
      }
    },
    {
      "person": {
        "name": "Coty",
        "address": {
          "street": {
            "name": "Monroe",
            "number": 323
          },
          "area": {
            "zone": "BA",
            "name": "Belgrano"
          }
        }
      }
    }
  ]
}
Output:
[
  {
    "name": "Italia",
    "number": 2164
  },
  {
    "name": "Monroe",
    "number": 323
  }
]

Multi-Value Selector (.*)

.* traverses objects and arrays to select the values of all matching keys and returns matching results in an array.

Multi-Value Selector Over an Object

.* returns an array with all the values whose key matches the expression.

For example, { "aString": "hello", "aNum": 2, "aBool" : false, "aString" : "world" }.*aString returns [ "hello", "world" ].

The following example returns the values of all user elements from the input payload.

DataWeave Script:
%dw 2.0
output application/json
---
payload.users.*user
Input Payload:
<users>
  <user>Mariano</user>
  <user>Martin</user>
  <user>Leandro</user>
</users>
Output:
[ "Mariano", "Martin", "Leandro" ]

Multi-Value Selector Over an Array

On arrays, .* works the same way as the single-value selector (.). For example, payload.people.person.address.*street and the example payload.people.person.address.street (from Single-Value Selector Over an Array) return the same results.

Descendants Selector (..keyName)

This selector is applied to the context using the form ..keyName and retrieves the values of all matching key-value pairs in the sub-tree under the current context. Regardless of the hierarchical structure these fields are organized in, they are all placed at the same level in the output.

In this example, all of the fields that match the key name are placed in a list called names regardless of their cardinality in the tree of the input data.

DataWeave Script:
%dw 2.0
output application/json
---
{ names: payload.people..name }
Input Payload:
{
  "people": {
    "person": {
      "name": "Nial",
      "address": {
        "street": {
          "name": "Italia",
          "number": 2164
        },
        "area": {
          "zone": "San Isidro",
          "name": "Martinez"
        }
      }
    }
  }
}
Output:
{
  "names": [
    "Nial",
    "Italia",
    "Martinez"
  ]
}

Key-Value Pair Selector (.&keyName)

The & selector retrieves both the keys and the values of all matching keys pairs in the current context. These are returned as an object, containing these retrieved keys and values.

DataWeave Script:
%dw 2.0
output application/xml
---
{
  users: payload.users.&user
}
Input Payload:
<?xml version='1.0' encoding='US-ASCII'?>
<users>
  <user>Mariano</user>
  <user>Martin</user>
  <user>Leandro</user>
  <admin>Admin</admin>
  <admin>org_owner</admin>
</users>
Output:
<?xml version='1.0' encoding='US-ASCII'?>
<users>
  <user>Mariano</user>
  <user>Martin</user>
  <user>Leandro</user>
</users>

Note that unlike the multi-value selector, the output of this selector is an object, where the original keys for each value are also extracted.

Select All the Descendant Key-Value Pairs

DataWeave Script:
%dw 2.0
output application/json
---
{ names: payload.people..&name }
Input Payload:
{
  "people": {
    "person": {
      "name": "Nial",
      "address": {
        "street": {
          "name": "Italia",
          "number": 2164
        },
        "area": {
          "zone": "San Isidro",
          "name": "Martinez"
        }
      }
    }
  }
}
Output:
{
  "names": [
    { "name": "Nial" },
    { "name": "Italia" },
    { "name": "Martinez" }
  ]
}

Convert an Array of Objects into an Object of Objects

DataWeave Script:
%dw 2.0
output application/xml
---
{
  names: (payload.people..&name) reduce (value, aggregator) -> aggregator ++ value
  //Alternative methods
  //names: payload.people..&name reduce ($$ ++ $)
  //names: {(payload.people..&name)}
}
Input Payload
{
  "people": {
    "person": {
      "name": "Nial",
      "address": {
        "street": {
          "name": "Italia",
          "number": 2164
        },
        "area": {
          "zone": "San Isidro",
          "name": "Martinez"
        }
      }
    }
  }
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<names>
  <name>Nial</name>
  <name>Italia</name>
  <name>Martinez</name>
</names>

Index Selector ([anIndex])

The index selector returns the element at the specified position. It can be applied over an array, object, or string.

Index Selector Over an Array

This selector can be applied to String literals, Arrays and Objects. In the case of Objects, the value of the key-value pair found at the index is returned. In the case of Arrays, the value of the element is returned. The index is zero-based.

  1. If the index is bigger or equal to 0, it starts counting from the beginning.

  2. If the index is negative, it starts counting from the end where -1 is the last element.

DataWeave Script:
%dw 2.0
output application/json
---
payload.people[1]
Input Payload
{
  "people": [
        {
          "nameFirst": "Nial",
          "nameLast": "Martinez"
        },
        {
          "nameFirst": "Coty",
          "nameLast": "Belgrano"
        }
    ]
}
Output:
{
  "nameFirst": "Coty",
  "nameLast": "Belgrano"
}

Index Selector Over an Object

The selector returns the value of the key-value pair at the specified position.

Transform
%dw 2.0
output application/json
---
payload[1]
Input:
{
  "nameFirst": "Mark",
  "nameLast": "Nguyen"
}
Output:
"Nguyen"

Index Selector Over a String

When using the Index Selector with a string, the string is broken down into an array, where each character is an index.

DataWeave Script:
output application/json
---
{ name: "MuleSoft"[0] }
Output:
{ "name": "M" }

The selector picks the character at a given position, treating the string as an array of characters.

  1. If the index is bigger or equal to 0, it starts counting from the beginning.

  2. If the index is negative, it starts counting from the end.

DataWeave Script:
%dw 2.0
output application/json
---
{ name: "Emiliano"[0] }
Output:
{
  "name": "E"
}

Range selector (anIndex to anotherIndex)

The range selector (to) returns matching indices of an array or string. You can also use it to reverse the order of the indices in the range. The selector treats characters in the string as indices.

Range Selector Over an Array

Range selectors limit the output to only the elements specified by the range on that specific order. This selector allows you to slice an array or even invert it.

DataWeave Script:
%dw 2.0
output application/json
---
{
  slice: [0,1,2][0 to 1],
  last: [0,1,2][-1 to 0]
}
Output:
{
  "slice": [
    0,
    1
  ],
  "last": [
    2,
    1,
    0
  ]
}

Range Selector Over a String

The Range selector limits the output to only the elements specified by the range on that specific order, treating the string as an array of characters. This selector allows you to slice a string or even invert it.

DataWeave Script:
%dw 2.0
output application/json
---
{
  slice: "DataWeave"[0 to 1],
  last: "DataWeave"[-1 to 0]
}
Output:
{
  "slice": "Da",
  "last": "evaeWataD"
}

XML Attribute Selector (.@keyName)

.@keyName selects an attribute in an XML element.

Using .@ without the key name returns an object containing the attributes as key-value pairs.

This DataWeave example reads an XML sample into a variable and uses @ to select attributes from the XML.

DataWeave Script:
%dw 2.0
var myVar = read('<product id="1" type="electronic">
  <brand>SomeBrand</brand>
</product>', 'application/xml')
output application/json
---
{
  item: [
  	{
      "type" : myVar.product.@."type",
      "name" : myVar.product.brand,
      "attributes": myVar.product.@
    }
  ]
}
Output:
{
  "item": [
    {
      "type": "electronic",
      "name": "SomeBrand",
      "attributes": {
        "id": "1",
        "type": "electronic"
      }
    }
  ]
}

Namespace Selector (#)

# returns the XML namespace of a selected key.

DataWeave Script:
%dw 2.0
output application/json
---
payload.order.#
Input Payload:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:order xmlns:ns0=http://orders.company.com>
  <name>Mark</name>
  <items>42</items>
  <orderdate>2017-01-04</orderdate>
</ns0:order>
Output:
"http://orders.company.com"

Selector Modifiers (!, ?)

You can check for the presence of a given key.

  • ! evaluates the selection and fails with an exception message if the key is not present.

  • ? returns true if the selected key is present, false if not. Note that ? is also used in Filter Selectors (keyName[?($ == "aValue")]).

Assert Present Validator

! returns an error if any of the specified key is missing.

  • { "name": "Annie" }.lastName! returns an error with the message, There is no key named 'lastName'.

  • Without the !, { "name": "Annie" }.lastName returns null.

  • When the key is present, { "name": "Annie" }.name! the result is "Annie".

Key Present Validator

Returns true if the specified key is present in the object or as an attribute of an XML element.

This example returns true because the name key does exists.

DataWeave Script:
%dw 2.0
output application/xml
---
present: payload.name?
Input Payload:
{ "name": "Annie" }
Output:
<?xml version="1.0" encoding="UTF-8"?>
<present>true</present>

? also works with XML attributes:

DataWeave Script:
%dw 2.0
output application/json
---
{
  item: {
    typePresent : payload.product.@."type"?
  }
}
Input Payload:
<product id="1" type="tv">
  <brand>Samsung</brand>
</product>
Output:
{
  "item": { "typePresent": true }
}

Filter Selectors (keyName[?($ == "aValue")])

keyName[?($ == "aValue")] returns only the values of matching keys within an array or object. Note that ? is also used in Key Present Validator. If no key-value pairs match, the result is null.

The following example inputs the array of name keys returned by *.name, then checks for name keys with the value "Mariano". It filters out any values that do not match. Note that the $ references the value of the selected key.

DataWeave Script:
%dw 2.0
output application/json
---
{ users: payload.users.*name[?($ == "Mariano")] }
Input Payload:
<users>
  <name>Mariano</name>
  <name>Luis</name>
  <name>Mariano</name>
</users>
Output:
{
  "users": [
    "Mariano",
    "Mariano"
  ]
}

The following example assumes the same Input Payload: and returns all the key-value pairs of the input because the expression ( 1 == 1 ) is true. Note that a false expression, such as ( 1 == 2 ), returns null.

DataWeave Script:
%dw 2.0
output application/json
---
{ users: payload.users.*name[?( 1 == 1)] }
Output:
{
  "users": [
    "Mariano",
    "Luis",
    "Mariano"
  ]
}

The following example assumes the same Input Payload:. It uses mapObject to iterates over the entire input object and returns matching key-value pairs, filtering out any pairs that do not match.

DataWeave Script:
%dw 2.0
output application/json
---
payload mapObject { ($$) : $[?($=="Mariano")] }
Output:
{
  "users": {
    "name": "Mariano",
    "name": "Mariano"
  }
}

Metadata Selector (.^someMetadata)

Examples in the subsections that follow use supported metadata selectors:

Class Metadata Selector (.^class)

Returns the class of the Plain Old Java Object (POJO). The value might result from calling a method in a Java class or have a data type (such as String or DateTime) that DataWeave treats as a Java value, for example:

  • { "string" : payload.mystring.^class } might return { "mystring": "java.lang.String" } if the input payload defines a Java string, such as simplePojo.string = "myString", in a simple POJO.

  • { "mydate" : payload.mydate.^class } might return { "mydate": "java.util.Date" }.

In the following Mule app flow, the Logger uses payload.^class to select the Java class of "my string", set in the Set Payload (set-payload) component.

Mule App XML in Anypoint Studio:
<flow name="setpayloadobjectFlow" >
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="15" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </scheduler>
  <!-- Set the payload to "my string". -->
  <set-payload value='"my string"' doc:name="Set Payload" />
  <!-- Select the class to which "my string" belongs. -->
  <logger level="INFO" doc:name="Logger" message="#[payload.^class]"/>
</flow>

The Studio console output shows that the payload string belongs to the class java.lang.String.

Console Output in Anypoint Studio:
INFO  2019-04-20 16:10:03,075 [[MuleRuntime].cpuLight.08:
 [setpayloadobject].setpayloadobjectFlow.CPU_LITE @6447187e]
 [event: 6da29400-63c1-11e9-98e0-8c8590a99d48]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 java.lang.String

Encoding Metadata Selector (.^encoding)

Returns the encoding of a value. For example, { "myEncoding" : payload.^encoding } might return {"myEncoding": "UTF-8"} for an input POJO.

In the following Mule app flow, the Logger uses payload.^encoding to select the encoding of "my string"`set in the Set Payload (`set-payload) component. The Scheduler (scheduler) component is simply an event source that regularly generates a new Mule event to hold the payload set in Set Payload.

Mule App XML in Anypoint Studio:
<flow name="setpayloadobjectFlow" >
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="15" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </scheduler>
  <!-- Set the payload to "my string". -->
  <set-payload value='"my string"' doc:name="Set Payload" />
  <!-- Select the encoding of "my string". -->
  <logger level="INFO" doc:name="Logger" message="#[payload.^encoding]"/>
</flow>

The Studio console output shows that the payload string has UTF-8 encoding.

Console Output in Anypoint Studio:
INFO  2019-04-20 16:14:24,222 [[MuleRuntime].cpuLight.03:
 [setpayloadobject].setpayloadobjectFlow.CPU_LITE @62bea6a6]
 [event: 0938bf70-63c2-11e9-98e0-8c8590a99d48]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 UTF-8

Media Type Metadata Selector (.^mediaType)

Returns the MIME type of a value that includes parameters (for example, application/json;charset=UTF-16). The expression in the value of { "myMediaType" : payload.^mediaType } might return "myMediaType": "/; charset=UTF-8" for an input POJO.

In the following Mule app flow, the Logger uses payload.^mediaType to select the media type of 2014-10-12T11:11:19-00:03 set in the Set Payload (set-payload) component.

Mule App XML in Anypoint Studio:
<flow name="setpayloadobjectFlow" >
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="15" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </scheduler>
  <set-payload value='#[|2014-10-12T11:11:19-00:03| as DateTime]' doc:name="Set Payload" />
  <logger level="INFO" doc:name="Logger" message="#[payload.^mediaType]"/>
</flow>

The Studio console output shows that the DateTime payload has the application/java; charset=UTF-8 media type.

Console Output in Anypoint Studio:
INFO  2019-04-20 16:41:01,276 [[MuleRuntime].cpuLight.04:
 [setpayloadobject].setpayloadobjectFlow.CPU_LITE @7e991c71]
 [event: c0e96860-63c5-11e9-bcff-8c8590a99d48]
 rg.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 application/java; charset=UTF-8

In the following Mule app flow, the Loggers use payload.^mediaType to select a string "my string", then to select a string that is set within an fx expression (#["my string as String type" as String]) in the Set Payload (set-payload) component.

Mule App XML in Anypoint Studio:
<flow name="setpayloadobjectFlow" >
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="15" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </scheduler>
  <!-- Set the payload to "my string". -->
  <set-payload value='"my string"' doc:name="Set Payload" />
  <!-- Select the media type of "my string". -->
  <logger level="INFO" doc:name="Logger" message='#[payload.^mediaType]'/>
  <!-- Set the payload using the fx expression "my string" as String. -->
  <set-payload value='#["my string as String type" as String]' doc:name="Set Payload" />
  <!-- Select the media type of a Java string. -->
  <logger level="INFO" doc:name="Logger" message='#[payload.^mediaType]'/>
</flow>

The Studio console output shows that the simple string has the media type /, while the string that is set in the fx expression has the media type application/java; charset=UTF-8.

Console Output in Anypoint Studio:
INFO  2019-04-20 16:52:50,801 [[MuleRuntime].cpuLight.01:
 [setpayloadobject].setpayloadobjectFlow.CPU_LITE @5d914abe]
 [event: 68121cd0-63c7-11e9-bcff-8c8590a99d48]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 */*

INFO  2019-04-20 16:52:51,085 [[MuleRuntime].cpuLight.01:
 [setpayloadobject].setpayloadobjectFlow.CPU_LITE @5d914abe]
 [event: 68121cd0-63c7-11e9-bcff-8c8590a99d48]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 application/java; charset=UTF-8

MIME Type Metadata Selector (.^mimeType)

Returns the MIME type (without parameters) of a value, for example, application/json, and { "myMimeType" : payload.^mimeType } might return { "myMediaType": "/" } for an input POJO.

In the following Mule app flow, the Loggers use payload.^mimeType to select a string "my string", then to select a string that is set within an fx expression (#["my string as String type" as String]) in the Set Payload (set-payload) component.

Mule App XML in Anypoint Studio:
<flow name="setpayloadobjectFlow" >
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="15" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </scheduler>
  <!-- Set the payload to "my string". -->
  <set-payload value='"my string"' doc:name="Set Payload" />
  <!-- Select the MIME type of "my string". -->
  <logger level="INFO" doc:name="Logger" message='#[payload.^mimeType]'/>
  <!-- Set the payload using the fx expression "my string" as String. -->
  <set-payload value='#["my string as String type" as String]' doc:name="Set Payload" />
  <!-- Select the MIME type of a Java string. -->
  <logger level="INFO" doc:name="Logger" message='#[payload.^mimeType]'/>
</flow>

The Studio console output shows that the simple string has the MIME type /, while the string that is set in the fx expression has the MIME type application/java.

Console Output in Anypoint Studio:
INFO  2019-04-20 17:02:07,762 [[MuleRuntime].cpuLight.06:
 [setpayloadobject].setpayloadobjectFlow.CPU_LITE @2d6f64b9]
 [event: b4097b00-63c8-11e9-bcff-8c8590a99d48]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 */*

INFO  2019-04-20 17:02:08,029 [[MuleRuntime].cpuLight.06:
 [setpayloadobject].setpayloadobjectFlow.CPU_LITE @2d6f64b9]
 [event: b4097b00-63c8-11e9-bcff-8c8590a99d48]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 application/java

Raw Metadata Selector (.^raw)

Returns the underlying binary value of a POJO. This selector is sometimes used when calculating an MD5 or some other cryptographic hash function to check for man-in-the-middle (MITM) attacks.

The following example uses the Set Payload component (set-payload) to produce a binary value, then uses the Transform Message component (ee:transform) component to return raw data for the MD5 (MD5(payload.^raw)) of the binary value. The Logger component (logger) is also set to write the raw data to the Studio console. For comparison, the second Logger returns the typical payload in a standard JSON format.

Mule App XML in Anypoint Studio:
<flow name="rawcaret2Flow" >
  <scheduler doc:name="Scheduler" >
    <scheduling-strategy >
      <fixed-frequency frequency="30" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </scheduler>
  <set-payload value='#["1234-5678-9123" as Binary]' doc:name="Set Payload" />
  <ee:transform doc:name="Transform Message" >
    <ee:message >
      <ee:set-payload ><![CDATA[%dw 2.0
import * from dw::Crypto
output application/json
---
{ "myRawData" : MD5(payload.^raw) }]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <logger level="INFO" doc:name="Logger" message="#[payload.^raw]"/>
  <logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>

Notice that instead of producing standard JSON output, the raw output in the Logger message surrounds the entire payload in double-quotes and inserts new line characters (\n) for each new line.

Console Output in Anypoint Studio:
INFO  2019-04-22 14:10:14,537 [[MuleRuntime].cpuLight.08:
 [rawcaret2].rawcaret2Flow.CPU_LITE @764a5a61]
 [event: 058f6a90-6543-11e9-9d99-8c8590a99d48]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 "{\n  "myRawData": "5403e5a202c594871d59898b13054be5"\n}"

INFO  2019-04-22 14:10:14,540 [[MuleRuntime].cpuLight.08:
 [rawcaret2].rawcaret2Flow.CPU_LITE @764a5a61]
 [event: 058f6a90-6543-11e9-9d99-8c8590a99d48]
 org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
 { "myRawData": "5403e5a202c594871d59898b13054be5" }

===

Custom Metadata Selector (.^myCustomMetadata)

Returns the value of custom metadata. Metadata can be associated with any value by using the as operator.

The following example uses userName.^myCustomMetadata to return the value of custom metadata that is defined as a variable (named userName) in the header of the script as a DataWeave script. For comparison, the example also returns the value of userName.

DataWeave Script:
%dw 2.0
output application/json
var userName = "DataWeave" as String {myCustomMetadata: "customMetadataValue"}
---

{
  "valueOfVariableMetaData" :  userName.^myCustomMetadata,
  "valueOfVariable" :  userName,
}

The output of the script is "customMetadataValue" for the value of the custom metadata and "DataWeave" for value of the userName variable.

Output:
{
  "valueOfVariableMetaData": "customMetadataValue",
  "valueOfVariable": "DataWeave"
}

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.