DataWeave 2 playground - Working with multiple Inputs, Vars, Attributes


DataWeave playground is one of my favorite tool for creating and trying out DataWeave scripts. Playground is very handy for writing some quick transformation scripts. Real world scripts will often work with more than one inputs, make use of flow variables, attributes or some custom modules. Can playground work with such scripts? Let’s explore it in this post.

DataWeave Playground

DataWeave Playground is a web browser based editor for creating dataweave scripts. It supports using mocked input payloads to run transformations and preview the output. I find it very handy and useful for trying out some dataweave scripts or expressions.

As you can see in following image of playground, it has few panels -

  1. Input Explorer: Mocked input payload for script

  2. Script Explorer: Transformation script list

  3. Script: The actual transformation script editor

  4. Output: Preview script execution results

  5. Log Viewer: (At bottom left corner) Displays dataweave logs generated by usage of log() function.

DataWeave Playground

Working with multi-input script

Consider following transformation scenario -

  1. Input JSON payload that should be converted to a XML format.

  2. While converting, we need to use currency conversion rates lookup data from a flow variable to calculate book price in different currencies.

  3. We should also filter the books published after the criteria provided in http query param publishedAfter.

Input JSON payload
[
  {
      "type": "book",
      "price": 30,
      "properties": {
        "title": "Everyday Italian",
        "author": [
          "Giada De Laurentiis"
        ],
        "year": 2005
      }
  },
  {
      "type": "book",
      "price": 29.99,
      "properties": {
        "title": "Harry Potter",
        "author": [
          "J K. Rowling"
        ],
        "year": 2005
      }
  },
  {
      "type": "book",
      "price": 41.12,
      "properties": {
        "title": "Mule in Action",
        "author": [
          "David Dossot",
          "John D'Emic"
        ],
        "year": 2009
      }
  },
  {
      "type": "book",
      "price": 49.99,
      "properties": {
        "title": "XQuery Kick Start",
        "author": [
          "James McGovern",
          "Per Bothner",
          "Kurt Cagle",
          "James Linn",
          "Kurt Cagle",
          "Vaidyanathan Nagarajan"
        ],
        "year": 2003
      }
  },
  {
      "type": "book",
      "price": 39.95,
      "properties": {
        "title": "Learning XML",
        "author": [
          "Erik T. Ray"
        ],
        "year": 2003
      }
  }
]

When working in studio transform message processor, we will have three inputs to use for this script - payload, vars.exchangeRate, and attributes.queryParams.publishedAfter.

Can we write that script using playground?

Yes, we can do that. Here are the steps -

Step 1: Add input json in payload of Input Explorer. If your payload has of different contnet-type, like csv, you can change it from the content type dropdown on input explorer.

DataWeave Playground 2

Step 2: Get back to the root of Input Explore by clicking the < at top left. Then click on '+' to add another input named vars.

DataWeave Playground 3

Step 3: Add another input named attributes to set query parameters.

DataWeave Playground 4

Your input explorer should have 3 inputs -

DataWeave Playground 5 - input explorer

Now you can write a script in playground that should look same as in studio transform message -

DataWeave playground 6 - Multi Input

Adding custom modules

If you want to try adding some custom modules, you can add it on Script explorer.

DataWeave Playground - Custom modules

Once you add that, you can import this module in your main script and use those functions like below -

%dw 2.0
output application/json
import toUpper from mymodule
---
toUpper("Manik")

Now go ahead and try your scripts on playground. If you are debugging some script, you can use log() function and open log viewer section to see log output.

You can also check dataweave related other posts on this site.

If you have any thoughts or feedback on this article, feel free to comment on this article or find me on Twitter or on manik.magar.me.

on twitter to get updates on new posts.

Stay updated!

On this blog, I post articles about different technologies like Java, MuleSoft, and much more.

You can get updates for new Posts in your email by subscribing to JavaStreets feed here -


Lives on Java Planet, Walks on Java Streets, Read/Writes in Java, JCP member, Jakarta EE enthusiast, MuleSoft Integration Architect, MuleSoft Community Ambassador, Open Source Contributor and Supporter, also writes at Unit Testers, A Family man!