"Cannot coerce Null to String" in mule 4 - anypoint-studio

%dw 2.0
import * from dw::core::Binaries
output application/json
var userCredentials = vars.userCredentials.userName as String ++ ":" ++ vars.userCredentials.password as String
---
"Basic "++ toBase64(userCredentials as String)
Error [MULE:EXPRESSION] while running test 'get-authorization-token-test-success':"Cannot coerce Null (org.mule.weave.v2.model.values.NullValue$#799da78a) to String
1| %dw 2.0 import * from dw::core::Binaries output application/json var userCredentials = vars.userCredentials.userName as String ++ ":" ++ vars.userCredentials.password as String --- "Basic "++ toBase64(userCredentials as String)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
at ++ (line: 1, column: 89)
at ++ (line: 1, column: 129)
at main (line: 1, column: 136)" evaluating expression: "%dw 2.0 import * from dw::core::Binaries output application/json var userCredentials = vars.userCredentials.userName as String ++ ":" ++ vars.userCredentials.password as String --- "Basic "++ toBase64(userCredentials as String)".

I'll assume that the question is what is causing the error shown. The error is self describing really: You can not concatenate a null with a String. The underlined part of the script indicates the location of the issue. The problem seems to be that either the variable vars.userCredentials is null or it doesn't contain an attribute called userName. You'll have to figure why it reaches the script with a null value and fix it.

Double check the value stored in the vars.userCredentials. This error comes when this value or its elements userName or password are null.
In your data weave, you have written the statement vars.userCredentials.userName as String, which in runtime gets executed as null as String ==> and hence Mule runtime engine is throwing an error.

Related

Array in payload is string, need to coerce to array type (DataWeave)

I have a payload I'm receiving from debezium/kafka as
"reasons": "[7,10,9]" with the array as a string.
I need to filter the array to extract when the item is 10 or 11. Since the array is actually a string in the payload I need to coerce it to an array to filter.
This is my current solution, but I feel there has to be a more efficient way:
%dw 2.0
output application/json
var data = payload.payload.after
var reasons = data.reasons replace "[" with "" replace "]" with "" splitBy "," filter ((num, numIndex) -> num != "10" and num != "11")
---
{
"dnsType": if (dnsType[0] == "11") "clinical" else if (dnsType[0] == "10") "non-clinical" else ""
}
If the string content is compatible with a JSON array then you can use the read() function to let DataWeave parse it for you.
Example read(data.reasons,"application/json")

How can I write this expression in DW 2.0 mule 4

I am using this expression in TM component in mule3 and I wants to migrate this in mule 4.Can anyone please help me to solve this issue
(payload splitBy "\n") map using (startIndex = 41,lastIndex = ((sizeOf $) - 1)){
"Data" : $[[startIndex][0] .. [lastIndex][0]]
I am getting missing expression error
using() is deprecated, replace with do-block
Unable to call: splitBy with arguments: (String | Null, "\\n").
Reasons:
Expecting Type: String, but got: Null. |-- From: String |- From: splitBy(text: String, separator: String) ->
Array<String> 4| (payload splitBy "\n") map using (startIndex =
41,lastIndex = ((sizeOf $) - 1)){
^^^^^^^
Expecting Type: String, but got: Null. |-- From: String
Expecting Type: Regex, but got: "\\n". |-- From: Regex |- From: splitBy(text: String, regex: Regex) ->
Array<String> 4| (payload splitBy "\n") map using (startIndex =
41,lastIndex = ((sizeOf $) - 1)){
^^^^^^^
when I tried same expression in mule 4
Thanks In advance to all who can try to help me
As stated you can change your expression to use do instead of using.
The .. will be replaced by to for getting the substring
%dw 2.0
output application/json
---
(payload splitBy "\n") map do {
var startIndex = 41
var lastIndex = sizeOf($) - 1
---
{
"Data" : $[[startIndex][0] to [lastIndex][0]]
}
}
Note that [startIndex][0] is same as startIndex so you can replace $[[startIndex][0] to [lastIndex][0]] with $[startIndex to lastIndex]
Also if the startIndex is really static and do not depend on the element, you can reduce your expression with
%dw 2.0
output application/json
var startIndex = 41
---
payload
splitBy "\n"
map {
"Data" : $[startIndex to (sizeOf($) -1)]
}

remove all the extra lines from the array in mule 4

I want to remove the extra lines or "\n\r" & "\n" from the array but my solution is not working. Please provide the correct function or dataweave for this.
input (json array format):
[{"m":"a\n\r",
"a":"b\n"},
{"m":"a\r\n",
"a":"b\n"}]
expected output(json array format):
[{"m":"a",
"a":"b"},
{"m":"a",
"a":"b"}]
code:
%dw 2.0
var someSpaceJson = write(payload, "application/json", {"indent":false})
output application/json
---
someSpaceJson replace "\n\r" with ""
You need to specify \\ instead of \ to represent the escape char.
%dw 2.0
var someSpaceJson = write(payload, "application/json", {"indent":false})
output application/json
---
read((someSpaceJson replace "\\r" with "" replace "\\n" with ""),"application/json")
This should give you your desired output.
If you want to preserve the new lines between the values and only want to remove the trailing \r's and \n's you can use the following. This will also avoid converting JSON to string and back which generally should be avoided.
%dw 2.0
output application/json
---
payload map ($ mapObject ($$): trim($))
However, you need to make sure that all values are string or null. If that is not the case you can add those conditions in the mapObject function itself.

string to date conversion using dataweave

Input String: 201801
Output String format: 01.2018
I tried using following but it's not working, I also looked up for string to date convesrion/coercion table in "Type Coercion Table" https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-types#type-coercion-table. I did not find something to help me with this.
as :date {format: "yyyyww"} as :string {format: "ww.yyyy"}
Appreciate if anyone has any ideas to share.
If you don't mind a bit of string hackery you could just move the various parts of the input string around:
%dw 1.0
%output application/json
%var inputDate = "201801"
---
{
outputDate: inputDate[4..5] ++ "." ++ inputDate[0..3]
}
The output of that is
{
"outputDate": "01.2018"
}
The above isn't null safe though, if there is a chance that the input date string is empty, or if it is shorter than 6 characters you will get a runtime error. You could work around that by checking the date in the DW, something like
outputDate: (inputDate[4..5] ++ "." ++ inputDate[0..3]) when inputDate != null and ( sizeOf inputDate ) == 6 otherwise null

How to append the first and last with " double quotes for dw variable in Dataweave mule

%output application/json
%var EmpIds=flowVars.empIDMap.EmpIds
---
payload map using(eNumber=$.EMPLOYEE) {
content: EmpIds.eNumber,
}
If i add value within double quotes i am getting value for example EmpIds."6" for
Need to append the first and last with " double quotes to get the value from EmpIds based on eNumber.
empIDMap data is :: {EmpIds={6=2, 19=3, 21=4, 36=5, 168421=6, 167727=7, 167729=8, 161759=9, 162052=10, 157633=11}}
can you please help on this.
To get value as string try eNumber= $.EMPLOYEE ++ "". This works fine.