Skip to main content

#MuleSoft DataWeave3 discussing

Questions and answers about MuleSoft DataWeave, best practices, and use cases.

%dw 2.0

 

import * from dw::core::Strings

 

var regExDateTime = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/

var regExDate = /^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/

var regExBoolean = /^(true|false)$/

var id= payload.ChangeEventHeader.recordIds[0]

 

output text/plain

---

id

My DW is somewhat big having some calculation for sake of simplicity I just pasted 4 lines of code only.

In that 4 line itself I am getting below error

Scripting language error on expression '%dw 2.0

 

import * from dw::core::Strings

 

var regExDateTime = /\d{4}-[01]\d-[...'. Reason: Unable to resolve reference of payload..

I even tried by defining meta data for payload as like below

 

var id= payload.ChangeEventHeader.recordIds[0] as String 
var id= payload.ChangeEventHeader.recordIds[0] as String  default 'axd123'

But nothing helps

3 answers
  1. May 27, 12:39 PM

    regarding specify the reader property to dataweave with input payload application/json in header , something like this example?  

    %dw 2.0 

    output application/json 

    input payload application/json 

    --- 

        myRootElement: payload 

    }

0/9000

I have requirement to call PATCH method of a resource where user can update of the details mentioned in the request . Account ID which is the key is passed as URI. User can update any or all of these parameters. Since request parameters are not fixed i need to dynamically build UPDATE query.

{

   "name": "Mule",

"phone": "888888888",

"balance": "2000",

"billingCity": "London"

}

Below is the dataweave that is written to build set query. I have tried all combinations to execute update query.

 

%dw 2.0

import * from dw::core::Strings

output application/json 

---

((({

name: payload.name default "",

phone: payload.phone default "",

balance: payload.balance default "",

billingcity: payload.billingCity default ""

}pluck (($$)++'='++"\""++($)++"\"") ) 

reduce(item, acc="")-> acc ++ (if(!isEmpty(substringAfter(item,"="))) item++"," else "")) substringBeforeLast(",")) as String 

 

DB query

 

UPDATE account

SET :query

WHERE idaccount = :accID

 

Input parameters

 

output application/java

---

{

query: vars.query,

accID: attributes.uriParams.accID as String

}

 

vars.query is expected as name="Max",phone="9999999999",balance="20000",billingcity="Bangalore" to work properly. However it is not working . Tried replacing escape character with "" but then it doesnt work.

 

"name=\"Max\",phone=\"9999999999\",balance=\"20000\",billingcity=\"Bangalore\""

 

Kindly let me know if any better ideas to make this work

13 answers
  1. Shekh Muenuddeen (NTTData) Forum Ambassador
    Nov 22, 2020, 9:01 AM

    Hey,

     

    Another way to do it first create query variable and store below DataWeave logic

    %dw 2.0

    output application/json

    var data = {

    "name": "Mule",

    "phone": "888888888",

    "balance": "2000",

    "billingCity": "London"

    }

    ---

    "UPDATE account SET " ++

    ((data filterObject ((value, key, index) -> (value != null and value != "")) mapObject ((value, key, index) -> {

    myData : (key as String) ++ " = :" ++ (key as String)

    })).*myData joinBy " AND ") ++ "WHERE idaccount = :accID"

    Its gives you query like below I have stored it query variable

    "UPDATE account SET name = :name AND phone = :phone AND balance = :balance AND billingCity = :billingCityWHERE idaccount = :accID"

    Another DataWeave to stored the input parameters its stored like below DataWeave

    %dw 2.0

    output application/json

    var data = {

    "name": "Mule",

    "phone": "888888888",

    "balance": "2000",

    "billingCity": "London"

    }

    ---

    {

    idaccount : "12345678"

    } ++

    (data filterObject ((value, key, index) -> (value != null and value != ""))) default {}

    Output

    {

    "idaccount": "12345678",

    "name": "Mule",

    "phone": "888888888",

    "balance": "2000",

    "billingCity": "London"

    }

    And Finally in the DataBase Update Operation I have provided the both variable query and inputparametes as below

    Hey, Another way to do it first create query variable and store below DataWeave logic%dw 2. 

    SO its dynamic with Paramterized.

     

    Regards,

    Shekh

0/9000

I am getting below error as I have a HTTP requester node where we have a PUT call so there is no response body.

 

So while creating inbound response we get below error on Transform node.

Unable to parse empty input, while reading `payload` as Json

 

If I do a setPaylod like below with empty body then it works.

<set-payload value="#{}" doc:name="Set Payload" doc:id="4d3edefb-a8c4-43af-bfa4-4625f536509f" />

 

However I am looking for a solution to handle this in the transform node itself.Unable to parse empty input, while reading `payload` as JsonPlease let me know how to handle this in transform node in the best possible way ?

11 answers
  1. Shekh Muenuddeen (NTTData) Forum Ambassador
    Nov 24, 2021, 4:01 PM

    Hey,

     

    check once with below code in dataweave 2 mule 4

    %dw 2.0

    output application/java

    ---

    if(!isEmpty(payload))

    payload

    else

    null

    As per article its seem content type issue, means request body without content type

    https://help.mulesoft.com/s/article/Unable-to-parse-empty-input-error-with-Dataweave-Mule-4

0/9000

Mule Dataweave - Converting XML to JSON Arrayunble to convert xml to json array in dataweave 2.0

 

output :

"employees":{

"employee":{

"name" : "abc"

},

"employee":{

"name" : "xyz"

}

}

 

expected output :

"employees":{

"employee" : [

{ "name" : "abc" },

{ "name" : "xyz" }

]

 

dataweave script :

 

%dw 2.0

output application/json

---

payload

 

5 answers
  1. Jan 11, 2019, 5:08 PM

    Hi @tejakkk ,

     

    Could you try using next?

     

    %dw 2.0

    output application/java

    ---

    {"employees":{

    "employee":

    (payload.employees.*employee map{

    "name":$.name

    } )

    }

    }

     

    I hope the above helps! If you have any doubt do not hesitate to contact me.

     

    Regards,

     

    Arianna Flores

0/9000

How to Skip attribute or key ( eg SSN, password - sensitive )while transforming the object using mapObject

 

Payload:

[

{

"personal_information": {

"first_name": "Emiliano",

"middle_name": "Romoaldo",

"last_name": "Lesende",

"ssn": "001-08-84382"

},

"login_information": {

"username": "3miliano",

"password": "mypassword1234"

}

},

{

"personal_information": {

"first_name": "Mariano",

"middle_name": "Toribio",

"last_name": "de Achaval",

"ssn": "002-05-34738"

},

"login_information": {

"username": "machaval",

"password": "mypassword4321"

}

}

]

 

Here I have to remove ssn, password key from.

 

write now I have personal_information, login_information but i hv more attribute on same level

 

looking for dynamic way : except personal_information, login_information other attributes doesnt have sensitive information.

 

I have written below dataweave expression but its giving error. Can someone explain what is the error and solution

 MY code:

 

%dw 2.0

output application/json

// I have to remove sesitive fields like ssn, password from payload

 

---

payload map (value, key)->{

(value mapObject(obj, objKey, index)->

objKey match {

 

case str if( objKey as String == "personal_information" ) -> ( obj- "ssn")

case str if( objKey as String == "personal_information" ) -> ( obj- "password")

else -> obj

}

 

}

4 answers
  1. Mar 13, 2023, 9:35 PM

    Hi @Vidyasagar Mundhe​ ,

     

    Check this script:

    Hi @Vidyasagar Mundhe​ , Check this script:%dw 2.

    %dw 2.4

    output application/json skipNullOn="everywhere"

    var payload = [

    {

    "personal_information": {

    "first_name": "Emiliano",

    "middle_name": "Romoaldo",

    "last_name": "Lesende",

    "ssn": "001-08-84382"

    },

    "login_information": {

    "username": "3miliano",

    "password": "mypassword1234"

    }

    },

    {

    "personal_information": {

    "first_name": "Mariano",

    "middle_name": "Toribio",

    "last_name": "de Achaval",

    "ssn": "002-05-34738"

    },

    "login_information": {

    "username": "machaval",

    "password": "mypassword4321"

    }

    }

    ]

     

    var sensitiveFields = ['ssn','password'] map upper($)

     

    fun processValue (value,key) = if(typeOf(value)~=Object) removeSensitiveData(value)

    else

    (if ((sensitiveFields contains upper(key))) null else value)

    fun removeSensitiveData(item) = item mapObject (value,key) -> (key): processValue(value,key)

     

    ---

    payload map removeSensitiveData($)

    Here I'm walking first through the array of elements, and per each element I'm doing the mapObject. Then I'm checking in a recursive way if the value is an object or not. Based on that I'm removing the content of the attribute if the key is in the list sensitiveFields. With the skipNullOn directive I'm removing it from the result.

    Be aware that it won't remove object in this code, only values.

     

    Hope it helps

     

    Good luck!

    Juan Cruz Basso

0/9000

Hi i want to store and reprocessed the records inside the parallel for each , i am using object store to store the record .

  1. how to call the flow again to process the failed record,after parallel for each stopped
  2. how to pass retretive(objec store) payload to request api
  3. how to stop the flow once all failed records are processed

is store and reterive both should present inside the parallel for each flow?parallel for each , reprocessed error records

10 answers
  1. Sep 15, 2023, 10:41 PM

    In my opinion, adding VM connector on your flow will achieve those requirements.

     

    Below is a sample flow to implement pagination which is roughly the same concept. VM listener will keep on firing while there is data published to it.

    In my opinion, adding VM connector on your flow will achieve those requirements. Below is a sample flow to implement pagination which is roughly the same concept. 

    Hope this helps...

0/9000

I want to create HashMap where key will be one filed and value will be whole object. Sample input is

 

{"Location": [{

"startDate": "1900-01-01T00:00:00",

"Code": "9001",

"abc": "text1"

},

{

"startDate": "1900-01-01T00:00:00",

"Code": "9002",

"abc" : "text2"

}]

}

 

Desired output with key as "Code" filed and value as whole location object. Is there any way to achieve this using dataweave. I have tried following but it produces array instead of HashMap.

 

%dw 1.0

%output application/java

---

location: payload.location map {

($.Code) : $

}

7 answers
  1. Jun 10, 2016, 9:14 AM

    Thanks.. It worked for me.

0/9000

[

{

  "Order1": [

      {

      "Case1": "abcd",

      "Case2": "def"

      }

],

  "Order2": [

       {

       "Case3": "abcd",

      }

]

}

]

 

Hello friends,

This is my sample json array. how to replace "abcd" to "xyz" without altering the other data?

 

Thanks

4 answers
  1. May 21, 2020, 11:10 AM

    Hi @Usersend 27​, you can use the below recursive function:

    %dw 1.0

    %output application/json

    %var in = [

    {

    "Order1": [

    {

    "Case1": "abcd",

    "Case2": "def"

    }

    ],

    "Order2": [

    {

    "Case3": "abcd"

    }

    ]

    }

    ]

    %function replaceValue(e,r,v)

    e match {

    :array -> $ map (value,index) -> replaceValue(value,r,v),

    :object -> $ mapObject (value,key) ->

    (key): v when (value == r)

    otherwise replaceValue(value,r,v),

    default -> e

    }

    ---

    replaceValue(in,"abcd","xyz")

    output:

    [

    {

    "Order1": [

    {

    "Case1": "xyz",

    "Case2": "def"

    }

    ],

    "Order2": [

    {

    "Case3": "xyz"

    }

    ]

    }

    ]

     

    Regards,

    Karolina

0/9000

Hi,

 

My request payload is coming as ["ABC","XYZ"]

 

I need O/P Payload to be in the below format :-

 

[

{

"data":"ABC"

},

{

"data":"XYZ"

}

]

 

Can anyone help me in doing this transformation preferably through dataweave

Thanks.

16 answers
  1. Jul 2, 2017, 3:56 PM

    @aradhanamohanty123 pls don't repeat the same answer that is already given by someone again

0/9000

I have input like below :

[{

"entries": [{

"firstName": "ruhi"

}]

}, {

"entries": [{

"firstName": "testing"

}]

}]

 

Above needs to be transformed like below

{

"entries": [{

"firstName": "ruhi"

}, {

"firstName": "tesing"

}]

}

11 answers
  1. Mar 6, 2017, 5:25 PM

    @senugula This works fine

     

    %dw 1.0

    %output application/json

    ---

    entries : flatten payload.entries

     

    Hope this helps

0/9000