Skip to main content

#MuleSoft DataWeave5 人正在讨论

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

My input is :

[ {

"Booking Loca" : [ "Seedict Sparango","Cando Wall Communities","Maul O'donor","Ahohn kappler" ]

} ]

 

in this input there is a chance to get Name coming like Maul O'donor but I need to replace one single quote to Maul O''donor

 

The final Output Required Like below :

[Booking Loca] in ('Seedict Sparango','Cando Wall Communities','Maul O''donor','Ahohn kappler')

 

for this I am using below Data weave logic but at beginning and ending not getting single quote instead

%dw 2.0

output application/java

import * from dw::core::Strings

---

"[Booking Loca] in ("++((flatten(payload."Booking Loca")) map (wrapWith($,"'")) joinBy ",") replace "'" with '"' replace '","' with "','"++ ")"

 

Here I have a struggle with a small transformation hence please help me

1 个回答
  1. 7月2日 06:19

    Hello, 

     

    Please try the below dataweave.  

     

    %dw 2.0

    output application/java

    ---

    "[Booking Loca] in (" ++

    ((payload[0]."Booking Loca" map ("'" ++ ($ replace "'" with "''") ++ "'")) joinBy ",")

    ++ ")" 

     

    Thanks!

0/9000

%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

4 个回答
  1. 6月14日 04:19

    You can simplify it. In Mule 4 DataWeave, you usually don’t need input payload application/json. Just use:

    %dw 2.0

    output application/json

    ---

    {

    myRootElement: payload

    }

    The payload type comes from the message metadata/header (content-type: application/json).

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 个回答
  1. Shekh Muenuddeen (NTTData) Forum Ambassador
    2020年11月22日 09:01

    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 个回答
  1. Shekh Muenuddeen (NTTData) Forum Ambassador
    2021年11月24日 16:01

    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 个回答
  1. 2019年1月11日 17:08

    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 个回答
  1. 2023年3月13日 21:35

    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 个回答
  1. 2023年9月15日 22:41

    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 个回答
  1. 2016年6月10日 09:14

    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 个回答
  1. 2020年5月21日 11:10

    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 个回答
  1. 2017年7月2日 15:56

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

0/9000