Nav

mapObject

mapObject({ (K)?: V }, (V, K, Number) → Object): Object

Iterates through key-value pairs within an object and returns an object. You can retrieve the key, value, or index of any key-value pair in the object.

This function is similar to map. However, instead of processing only values of an object, mapObject processes both keys and values as a tuple. Also, instead of returning an array with the results of processing these values through the function, it returns an object, which consists of a list of the key-value pairs that result from processing both key and value of the object through the function (a lambda).

The function is invoked with three parameters: value, key and the index. The third parameter, index, is optional.

Transform

        
     
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
output application/json
var conversionRate=13
---
priceList: payload.prices mapObject(value, key, index) -> {
  (key) : {
       dollars: value,
       localCurrency: value * conversionRate,
       index_plus_1: index + 1
   }
}
Input

        
     
1
2
3
4
5
<prices>
    <basic>9.99</basic>
    <premium>53</premium>
    <vip>398.99</vip>
</prices>
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
  'priceList': {
    'basic': {
      'dollars': '9.99',
      'localCurrency': 129.87,
      'index_plus_1': 1
    },
    'premium': {
      'dollars': '53',
      'localCurrency': 689,
      'index_plus_1': 2
    },
    'vip': {
      'dollars': '398.99',
      'localCurrency': 5186.87,
      'index_plus_1': 3
    }
  }
}

For each key-value pair in the input in the example, the key is preserved, and the value becomes an object with two properties: the original value and the result of multiplying the original value by a constant, a var that is defined as a directive in the header of the DataWeave script.

Important: When you use a parameter to populate one of the keys of your output, you must either enclose the parameter in parentheses (for example, (key)), or you need to prepend it with a $ and enclose it in quotation marks (for example, '$key', as shown above). Otherwise, the name of the property is treated as a literal string.

If you do not name the parameters, you need to reference them through placeholders: $ for the value, $$ for the key, and $$$ for the index, for example:

Transform
%dw 2.0
output application/xml
---
{ 'price' : 9 } mapObject('$$' : $)
Output
<?xml version='1.0' encoding='UTF-8'?>
<price>9</price>

The next example incorporates each index of the //* price input above, as well as the input keys and values.

Transform
%dw 2.0
output application/json
---
priceList: payload.prices mapObject(
 ($$) : { '$$$' : $ }
)

Notice that the index is surrounded in quotes ('$$$') because this Numeric key must be coerced to a String and cannot be a Number. Alternatively, you could write '$$$' as ($$$ as String).

When the preceding script receives the //* price input above, it produces the following output:

Output
{
  'priceList': {
    'basic': {
      '0': '9.99'
    },
    'premium': {
      '1': '53'
    },
    'vip': {
      '2': '398.99'
    }
  }
}

The next example returns the same output as the first //* mapObject example above, which explicitly invokes the named parameters (value, key, index).

Transform
%dw 2.0
output application/json
var conversionRate=13
---
priceList: payload.prices mapObject(
 ($$): {
   dollars: $,
   localCurrency: $ * conversionRate,
   index_plus_1: $$$ + 1
 }
)

When you use a parameter to populate one of the keys of your output, as with the case of $$, you must either enclose it in quote marks (for example, '$$') or parentheses (for example, ($$)). Both are equally valid.

mapObject(Null, (Any, Any, Number) → Any): Null

Helper function that allows mapObject to work with null values.

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.

+