Using Dataweave Editor for Designing the APIs

Author: Rashmi Gilalkar

For most of the beginners in Mule 4, Dataweave can be a hurdle when they actually start designing their APIs. But, writing Dataweave scripts is what most us as developers will be doing maximum time during API development. So let’s see a simple example here that we will be referring throughout this article.


We have a JSON response(Resp.json) from another API and we need to extract a part of it and store in a variable(resultId). 


            "id": "123445555",
            "class": [
                    "id": "123445566",
                    “docUrl”: “”,
                    "type": "Student",
                    "name": {
                        "firstName": "Student1",
                        "lastName": "Student2"
                   "phoneNumbers": []

So we want to extract the string “RESULTID” from the docUrl field of the JSON response using a dataweave script. For this, we write a script but are not able to test the result there itself. So, we need to debug our application and check.

In such cases, we can use DataweaveEditor available at It helps us to run a dw script with dynamic input payload and help us save some time during development.

Let’s see how it works.

  1. Access the above  DataweaveEditor URL. There you’ll see three sections- Payload, Script, and Output as below.
  1. Select the input type of your script if you are going to use any input. In the above image, you see the JSON as input type and other available content-types available.
  2. Paste payload you want to transform in the PAYLOAD section, just below the highlighted PAYLOAD tag.
  3. In the SCRIPT section, comes the dataweave script you want to write to transform the payload.
  4. Check if your script gives the output/errors that you are expecting in the OUTPUT section above.
  5. Here is one thing that we need to take care of, that is, when we are using a script, it knows only what’s defined in payload, if any other variables or values are referred from other parts of your application then those need to be provided with correct hardcoded values to it, to get this working.

Let’s see how the scenario mentioned above can be accomplished using this dataweave editor: 

  1. So, we’ve added the payload in the PAYLOAD section and now we will first try to get the docUrl using the below script. As you can see, we got a string in the OUTPUT section, with the value of the url .
  1. Now we can try to extract the last part of this string using regex, which might need some trial and error in case you are not sure. So we can do that here. 
  1. We read the url in ‘url’ var and then extract the desired string “RESULTID” using ‘temp’ variable which stores the outcome of the regex. Now, this same  script can be used in the application as it is or with minor changes in variable assignments as per your application names.

As you can see now we are certain that the dw we are using will serve our purpose correctly. We are looking at a simple example here, but this works really well when we have pretty big dw and we do not want to run our entire code just to validate our dw scripts.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.