Transforming Data10% of the exam
Coercing, formatting, ordering and filtering in DataWeave
How the `as` operator coerces values with format, locale and mode properties, which date pattern letters mean what (uuuu, MM, dd, D, mm), how relational and equality operators coerce mixed types, how orderBy sorts ascending and how to reverse it, and when to use filter versus filterObject.
Apply correct syntax to write DataWeave transformations to coerce, format, order, and filter data, and predict outcomes.
~7 min read · 9 practice questions ·
What the exam expects
- Coerce values with
as, addingformat,localeormodeproperties, and predict the resulting type and text. - Parse a non-ISO date string with the matching input pattern, then write it in another pattern, choosing the right letters (
uuuu,yyyy,MM,dd,D,mm). - Predict comparisons between mixed types (
"12" > 8,"true" == true) and fix them withasor~=. - Sort with
orderBy, reverse the order for descending results, and predictorderByon an object. - Filter arrays with
filter(item and index) and objects withfilterObject(value, key and index). - Key terms: coercion (converting a value to another type with
asat runtime); format (a Java DecimalFormat orjava.timepattern on a coercion); mode (date parsing strictness: SMART, STRICT, LENIENT); criteria (the lambda result thatorderBysorts on orfiltertests); range selector ([-1 to 0], which reverses an array).
Key facts
Coercion properties
- Detail
class,format,locale,mode,unit
Number to String
- Detail
22 as String {format: ".00"}returns"22.00"
Trailing zeroes
- Detail
- Since DataWeave 2.5, writing numbers directly strips trailing zeroes
Date literal
- Detail
- ISO-8601,
Dateis|uuuu-MM-dd|
mode values- Detail
- SMART, STRICT, LENIENT; only STRICT errors on invalid dates
Year letters
- Detail
u= year (recommended),y= year of era,uu= two digits
Other letters
- Detail
D= day of year,M= month,d= day of month,m= minute
Relational mixed types
- Detail
- Right operand coerced to the left operand's type
== vs ~=- Detail
"true" == trueis false;"true" ~= trueis true
orderBy direction- Detail
- Ascending only; reverse with
[-1 to 0]
filter lambda- Detail
(item, index); keeps items where it returns true
filterObject lambda- Detail
(value, key, index); returns an object
Sources for this section (7)
- Type Coercion with DataWeavedocs.mulesoft.com (opens in a new tab)
- DataWeave Typesdocs.mulesoft.com (opens in a new tab)
- Format Dates and Timesdocs.mulesoft.com (opens in a new tab)
- DataWeave Operatorsdocs.mulesoft.com (opens in a new tab)
- orderBydocs.mulesoft.com (opens in a new tab)
- filterdocs.mulesoft.com (opens in a new tab)
- filterObjectdocs.mulesoft.com (opens in a new tab)
- Why does
"12" > 8evaluate to false? - When should you add
mode: "STRICT"to a date coercion? - Why does
[3,2,3] orderBy $return[2,3,3]rather than[3,3,2]? - When does a number coerced with
as String {format: ".00"}keep its trailing zeroes? - Why is
uuuupreferred overyyyyfor years?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the MuleSoft Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 40 lessons of the MuleSoft Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Practice exams purchase adds inline bank questions and topic practice (9 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams