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 of us as developers will be doing maximum time during API development. So let’s see a simple example here that we will be referring to throughout this article.
Scenario:
We have a JSON response(Resp.json) from another API and we need to extract a part of it and store it in a variable(resultId).
Resp.json:
{
"data":
[
{
"id": "123445555",
"class": [
{
"id": "123445566",
“docUrl”: “ https://testurl.com/welcome/RESULTID”,
"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 http://34.205.75.56:8081/. 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.
- Access the above DataweaveEditor URL. There you’ll see three sections- Payload, Script, and Output as below.
- 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.
- Paste payload you want to transform in the PAYLOAD section, just below the highlighted PAYLOAD tag.
- In the SCRIPT section, comes the dataweave script you want to write to transform the payload.
- Check if your script gives the output/errors that you are expecting in the OUTPUT section above.
- 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:
- 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 .
- 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.
- 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.
Reference: https://www.apisero.com/using-dataweave-editor-for-designing-the-apis/