Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Last active May 22, 2021 02:22
Show Gist options
  • Save aimtiaz11/ca0f2f6295bfa75098d9622d58068459 to your computer and use it in GitHub Desktop.
Save aimtiaz11/ca0f2f6295bfa75098d9622d58068459 to your computer and use it in GitHub Desktop.
Mule DW - practical examples

Dataweave Hack 1 - Append underscore or character to string at particular pos

Mule 3:

%dw 1.0
%output application/json
---
"cafm" splitBy "" map ($ ++ "_" when $$ == 0 otherwise $) reduce ($$ ++ $)

Mule 4:

%dw 2.0
output application/json
---
"cfam" splitBy "" map (if ($$ == 0) $ ++ "_" else $) reduce ($$ ++ $)

Dataweave Hack 2 - Read Cookie value from Cookie header

The following is a helpful Dataweave (2.0) code to allow you to extract cookie value coming in via Cookie header.

Script:

%dw 2.0
output application/java
---
(payload splitBy  "; " map ($ splitBy "=") reduce (item, accum = {}) -> (accum ++ { (item[0]): item[1] })) ['csrftoken']

Input

"PHPSESSID=413; csrftoken=444t4#tb3gg43; _gat=1"

Output

444t4#tb3gg43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment