Reading Data

Reading Data

In the previous tutorial, we reviewed how you can create data using Strings, Numbers, Boolean’s, Arrays, and Objects. Creating data is only half of DataWeave, however; reading data is just as important, and the features available to do so are just as robust.

DataWeave selectors allow navigating any combination of Objects and Arrays to get to the data you need. Throughout this tutorial we will review some of the most important ones:

  • Single-value selector: .
  • Index selector: [n]
  • Range selector: [n to m]
  • Multi-value selector: .*
  • Descendants selector: ..
  • XML Attribute Selector (.@myKey)
  • Namespace Selector (#)
  • Selector Modifiers (!, ?)
  • Filter Selectors (myKey[?($ == “aValue”)])
  • Metadata Selector (.^someMetadata)

You can also think of selectors as a way to query your data.

Extract Data

DataWeave can select data from DataWeave objects and arrays, variables that store that data, and the output of DataWeave functions when that output is an array or object. Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For other DataWeave versions, you can use the version selector in the DataWeave table of contents.

More precisely, a DataWeave 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. You can use selectors in Mule modules, connectors, and components that accept DataWeave expressions.

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.

Supported variable references areMule Runtime Variables, such as payload and attributes, and DataWeave Variables that store arrays or objects. A simple example is payload.myKey where the payload is the object {“myKey” : 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”.

Use Selectors on DataWeave Arrays and Objects

In DataWeave, selectors extract values from within a DataWeave object (such as { myKey : “myValue” }) or array (such as [1,2,3,4] or [ { “myKey” : “1234” }, { “name” : “somebody” } ]).

The following DataWeave script uses the single-value selector (.) to retrieve values from the object and array defined by the variables myObject and myArray.

DataWeave Script:

%dw 2.0

var myObject = { “myKey” : “1234”, “name” : “somebody” }

var myArray = [ { “myKey” : “1234” }, { “name” : “somebody” } ]

output application/json

{

    selectingValueUsingKeyInObject : myObject.name,

    selectingValueUsingKeyOfObjectInArray : myArray.name,

}

Output JSON:

{

    “selectingValueUsingKeyInObject”: “somebody”,

    “selectingValueUsingKeyOfObjectInArray”: [ “somebody” ]

 }

As the Output shows:

  • myObject.name returns the value “somebody”
  • myArray.name returns the array [ “somebody” ]

Selector Quick Reference

Single Value (.)

Acts on arrays and objects to return the value of a matching key. The syntax is .myKey. To retrieve values of duplicate keys in a DataWeave object, use *, instead.

Index ([])

Returns the value at the specified index of an array. An example is the [0] at the end of [“a”,”b”,”c”][0], which returns “a”. For examples, see Index Selector ([anIndex]).

Range [index1 to index2]

Returns an array with values of the selected indices. An example is the [2 to 3] at the end of [“a”,”b”,”c”,”d”][2 to 3], which returns [“c”,”d”]. For examples, see Range selector (anIndex to anotherIndex).

Multiple Values (*)

Acts on arrays and objects to retrieve the values of all matching keys at a single level in the hierarchy of a data structure. The syntax is *myKey. For the values of all duplicate keys at lower levels in the hierarchy, use the descendants selector (..), instead. For examples, see Multi-Value Selector (.*).

Descendants (..)

Acts on arrays and objects to retrieve all matching keys from arrays and objects below the given key, regardless of their location in the hierarchy. The syntax is ..myKey For examples, see Descendants Selector (..myKey).

Key-Value Pair (&)

Acts on arrays and objects. Instead of returning the value of the DataWeave object, this selector returns the entire DataWeave object, both key and value. The syntax is .&myKey. For examples, see Key-Value Pair Selector (.&myKey).

XML attribute (.@myKey)

Returns the value of a selected key for an XML attribute. For an example, see XML Attribute Selector (.@myKey).

Namespace Selector (myKey.#)

Returns the xmlns namespace from the element that also contains the selected key. 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. For examples, see Selector Modifiers (!, ?).

Filter Selectors (myKey[?(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. For examples, see Filter Selectors (myKey[?($ == “aValue”)]).

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), content length (.^contentLength), encoding (.^encoding), MIME type (.^mimeType), media type (.^mediaType), raw (.^raw), and custom (.^myCustomMetadata) metadata. For details, see Metadata Selector (.^someMetadata).

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *