VKP
VKP

Reputation: 658

How to get the first value from an Array after split String in Data weave?

I am trying to compare the first value in the string array after split the string . But my sub function throwing error saying it receiving an array , What i am doing wrong .

fun getErrorList() =
compareAddress(loanData.loan.propertyAddress,payload.propertyAddress)
fun compareAddress(loanpropertyAddress, inputPropertyAddress) =
if(null != loanpropertyAddress and null != inputPropertyAddress)
getAddr1 (loanpropertyAddress splitBy(" "))[0]
==
getAddr1 (inputPropertyAddress splitBy(" "))[0]
else
null

fun getAddr1(addr1) =
addr1 replace "#" with ("")

enter image description here

Upvotes: 0

Views: 2472

Answers (1)

olamiral
olamiral

Reputation: 1296

The problem is how you are calling the getAddr1 function. In the DataWeave expression you provided, your passing to the getAddr1 function an array of strings instead of a string:

...
getAddr1(
    loadpropertyAddress splitBy(" ") // splitBy returns an array of strings
)[0] // here, you're trying to get the first element of the value returned by getAddr1
...

I'm assuming you are trying to compare the first part of loan and input property addresses after removing "#" characters. If my understanding is correct, then you can do the following changes to your function:

...
getAddr1(
    loadpropertyAddress splitBy(" ")[0] // get the first element of the string array returned by the splitBy function
) // removed array item selector ([0])
...

With that modification, your DataWeave expression should look like:

fun getErrorList() =
compareAddress(loanData.loan.propertyAddress,payload.propertyAddress)
fun compareAddress(loanpropertyAddress, inputPropertyAddress) =
if(null != loanpropertyAddress and null != inputPropertyAddress)
getAddr1 (loanpropertyAddress splitBy(" ")[0])
==
getAddr1 (inputPropertyAddress splitBy(" ")[0])
else
null

fun getAddr1(addr1) =
addr1 replace "#" with ("")

Upvotes: 2

Related Questions