Ravindar, Integrations with Mulesoft & BizTalk

Just another Integration blog

“Unknown class or illegal statement” error with special characters in payload

We’ve faced an issue in a particular payload message.

one of the json fields value in the payload has special charaters # and [ and ]

like below

{
   "test":  "This is#[ hello world]. yes"
}

The api kit router was rejecting this message with an error

“Unknown class or illegal statement”

if you notice in the above message it has sysmbols # and [ and ] in the correct order it consider it as a mule expression.

following were the observations

1. The issue is only when you use API kit router. API kit router is rejecting such messages. Without API kit router mule accepts the message normal.

2. The issue is because API kit router unable to parse #[text….] in the message as it considered it as a MEL

3. it’s because of following 2 reasons. Without the following two things, the issue won’t happen
a) the two characters #[ should be together.(even no space)
b) There should be a closing bracket ] in the text somewhere after #[

when the two things are satisfied, the Apikit router cannot processes the message.

September 27, 2019 Posted by | Mule 3, Mulesoft | Leave a comment

Set a boolean or any specific data type in variable shape

To set a boolean variable to true you can use the following code. We need to specify the output datatape to be.. :boolean

#[dw("true as :boolean")]

even the following statement will output a boolean value. (true).(notice it’s not inside dataweave command and no double quotes)
or you can mention any other statement which equates to true

#[1==1]

the following will equates to false. (or you can put any text value like #[abc!=abc]

#[1!=1]

September 27, 2019 Posted by | Mule 3 | Leave a comment

Enrich payload with new fields using Message Enricher

Observation Notes:

We had a requirement of augmenting the current payload with few extra fields which we need to get from external service and then augment it to current payload.

We’ve used Message Enricher.
For example, we have a incoming Json payload like below


{
	"EmployeeID": "92"
}

Now you want to enrich your payload with extra fields like below. The two extra fields will have to get from external service and augment it to the original payload

{
	"EmployeeID": "92",
	"Name": "Test Name",
	"Location": "Test Location"
}

The flow goes like this

Message Enricher Smaple flow

The message enricher receives the copy of original message payload with just EmployeeID. The message enricher has two components here
1. Http call to exernal service to get the extra details of the employee
2. A transform shape (this is just to convert the response to java object). The transform shape code is like below. It just assigns the payload AS-IS, just changes to java object

%dw 1.0
%output application/java
---
payload

Now in the message enricher properties, set it like below

Message Enricher properties

Since we are enriching/augumenting only specific fields, we should use the “Enrich elements” section. And in that, the “Source” section indicates the payload inside the Enricher component (which is the response we got from the http call). And the “Target” section on the right side indicates the payload of the main flow.
so here, it will pick the “location” and “name” fields from the response of the http call, and adds those fields as “EmployeeLocation” and “EmployeeName” in the payload of the main flow.

so the result of the main flow final payload will be as desired like below

{
	"EmployeeID": "92",
	"Location": "Test Location",
	"EmployeeName": "Test Name"
}

Other regular usage of Enricher components are like

Another CASE I:
We want to retain the main payload As-IS but would like to store the response of the http call into a flow variable. Then set the properties of the enricher like below

Message Enricher properties_flowvar

So here, notice that in the source field we did not mention anything, in this case, after the Enricher component is executed, the output of the enricher component will be stored in the flow variable “postEnrich” as named above. However the main flow payload will still remain the same as the old original payload as below


{
	"EmployeeID": "92"
}

Another CASE 2:
well…we can play with it in many ways….

September 26, 2019 Posted by | Mule 3, Mulesoft | Leave a comment

Using variables in Dataweave 1.0

Observation Notes:

Case 1: Using just variable name to assign to another field
here declared a variable named “category” with value as “Employee”

%dw 1.0
%output application/java
%var category= "Employee"
--- 
{
   Section: category
}

In the above case, using the just variable name as-is will work. It will assign the value “Employee” to the Section.

Case 2: You want to use the variable value somewhere else code


%dw 1.0
%output application/java
%var category= "Employee"
--- 
{
   Section: category
   Location: payload."$(category)".Name
}

if you notice above, now the variable has been used in double quotest with $ symbol to get the value of the variable “$(category)”

September 23, 2019 Posted by | Mule 3, Mulesoft | Leave a comment

Search for a key with a specific value and pick that corresponding record’s values

We had a special requirement of searching json values (instead of keys) and pick the corresponding record other keys value

for example in the following example, products is an array and the json keys names inside the products.sections for each array item are dynamic. Means the keys can come like sec_Name1 or sec_Name3 or any other key name dynamically.

{
  "products": [
    {
      "code": "1",
      "name_1": "2",
      "area_A": "A1",
      "sections": null
    },
    {
      "code": "6",
      "name_2": "B2",
      "area_B": "B2",
      "sections": {
        "sec_Name3": "test3",
        "sec_Name2": "test4",
        "sec_name_n": "test..n"
      }
    },
    {
      "code": "9",
      "name_2": "B2",
      "area_B": "A1",
      "area_C": "A1",
      "sections": {
        "sec_Name3": "test2",
        "sec_Name2": "test4",
        "sec_name_n": "test..n"
      }
    }
  ]
}

Now the requirement was to search if any of keys inside the “sections” has a specific value say “test2” for example, then pick the Product.Code.

So two things
1. we need to search all the keys under “sections”
2. And pick “code” value which is one level up

I’ve used the following 2 approached to pick the value.

you can either use a dataweave expression like below

#[dw('(payload.products map $ filter (($.sections != null) and ($.sections contains "test2"))).code[0]')]

or you can use dataweave trasnformation like below

%dw 1.0
%output application/java
---
{
   Code: (payload.products map $ filter (($.sections != null) and ($.sections contains "test2"))).code[0]
}

probably there are many more ways to achieve this, but these ones worked for me

September 19, 2019 Posted by | Mule 3, Mulesoft | | Leave a comment

Multiple conditions in choice router with extra operation

Dataweave 1.0

I had a requirement to put multiple conditions in choice router but the values should be converted to upper before comparison

for example the payload.Action field can contain “Add” or “Update” fields.
but we want to allow it even if it’s passed in upper case like “ADD” or “UPDATE”

So I’ve used the following

#[dw('upper payload.Action')=='ADD' || dw('upper payload.Action')=='UPDATE']

September 18, 2019 Posted by | Mule 3, Mulesoft | Leave a comment

#[message.payloadAs(java.lang.String)]

#[message.payloadAs(java.lang.String)]

September 16, 2019 Posted by | Mule 3, Mulesoft | Leave a comment

Using enum in raml

This is for my notes. There are 2 ways to specify enum in raml


/MyResource:
  get:
    queryParameters:
      destination:
        required: true
        enum: 
          - SFO
          - LAX
          - CLE

alternativey we can specify enum as below


/MyResource:
  get:
    queryParameters:
      destination:
        required: true
        enum: [SFO, LAX, CLE]

September 15, 2019 Posted by | Mule 3, Mulesoft | Leave a comment

Delcare an empty array and add elements in the mule flow

We had a requirement to initialise an empty array and then collect(or add) items to it in the flow and at the end create a for loop for each of the items in that array

so we declared a flow variable with the value

#[[]]

the “[]” inside the #[] creates an empty array.

And then you can add items into this array by

flowVars.VariableName.add("your content here"); 

And at the end when you want to loop through this array, in the foreach set the this variable name flowVars.VariableName

on the other note(not related to above array), just if we want to set the pay load to null, then in setpayload shape use this

#[null]

September 12, 2019 Posted by | Mule 3, Mulesoft | Leave a comment

Replace some json keys in the payload keeping other keys as is

We had a requirement to replace few json keys in the payload. Where ever those keys are in the payload, they should be replaced with the new name.
For example

{
	"id": "1001402",
	"Details": [
		{
			"Person": [
				{
					"Name": "Person 1",
					"Address": "test Address1"
				},
				{
					"Name": "Person 1",
					"Address": "test Address2"
				}
			]
		}
	]
}

In the above json, if we want the keys “Name” ad “Address” to be renamed as “PersonName” and address as “PersonAddress”

we can use the following block of code which used a function

%dw 1.0
%output application/json

%function applyToKeys(e, fn)
  e match {
    :array  -> $ map ((v) -> applyToKeys(v, fn)),
    :object -> $ mapObject ((v, k) -> {(fn(k)): applyToKeys(v, fn)}),
    default -> $
  }
---
applyToKeys(payload,
            ((key) -> "PersonName" when ((key as :string) contains "Name")
                      otherwise "PersonAddress" when ((key as :string) contains "Address")
                      otherwise key)) 

I’ve got the function from the following urls and it helped me to change the code per my need
https://stackoverflow.com/questions/51792700/modify-existing-json-key-mule
https://www.jerney.io/dataweave-applywhenkey-function/

and the output is

{
	"id": "1001402",
	"Details": [
		{
			"Person": [
				{
					"PersonName": "Person 1",
					"PersonAddress": "test Address1"
				},
				{
					"PersonName": "Person 1",
					"PersonAddress": "test Address2"
				}
			]
		}
	]
}

September 10, 2019 Posted by | Mule 3, Mulesoft | 3 Comments