Skip to main content

#DataWeave 20 人正在讨论

how to convert following details using DWL 2.0 

input 

"{ name= John, age= 30, city= New York, image= { 

    url= test, 

    width= 200, 

    height= 200 

  } }" 

 

expected output 

{ "name": "John", "age": 30, "city": "New York","image": { 

    "url": "test", 

    "width": 200, 

    "height": 200 

  } } 

 

#MuleSoft DataWeave  #DataWeave 2

0/9000

Hi Team,

 

i have a job that run's everyday, i want to filter the array objects and extract the records that were modified, created in last 24hrs. i want to check all these 4 fields "C_Created_Date", "C_Updated_Date", "A_Created_Date" ,"A_Updated_Date".

 

If any one of these date fall under last 24hrs, i want to filter those records. Can you please assist.

 

input:

[

{

"Id": 212449,

"C_Created_Date": "2018-08-08 21:24:53.284",

"C_Updated_Date": "2023-10-31 15:34:38.926",

"A_Created_Date": "2018-08-08 21:13:13.809",

"A_Updated_Date": ""

},

{

"Id": 212439,

"C_Created_Date": "2018-08-08 22:27:47.442",

"C_Updated_Date": "2023-02-07 14:44:04.138",

"A_Created_Date": "2018-08-08 22:27:42.146",

"A_Updated_Date": "2023-10-31 17:34:12.853"

},

{

"Id": 21234449,

"C_Created_Date": "2018-08-08 22:30:21.739",

"C_Updated_Date": "2023-02-07 14:44:04.138",

"A_Created_Date": "2018-08-08 22:30:16.766",

"A_Updated_Date": ""

},

{

"Id": 423233,

"C_Created_Date": "2018-08-10 17:51:11.862",

"C_Updated_Date": "2023-02-07 14:44:04.138",

"A_Created_Date": "2018-08-10 17:51:07.438",

"A_Updated_Date": ""

},

{

"Id": 56462148,

"C_Created_Date": "2018-09-13 20:58:37.459",

"C_Updated_Date": "2023-02-07 14:44:04.138",

"A_Created_Date": "2018-09-13 20:57:09.180",

"A_Updated_Date": ""

},

{

"Id": 112324,

"C_Created_Date": "2018-09-21 20:43:37.095",

"C_Updated_Date": "2023-02-07 14:44:04.138",

"A_Created_Date": "2018-09-21 20:42:20.070",

"A_Updated_Date": ""

}

]

#DataWeave 2​ 

3 个回答
0/9000

Hi Team,

 

i'm seeking help with DWL. I want to create an array of arrays, where each array will have multiple objects and sum of all these objects "Active_Contracts_Count" should be less than 100.

 

Which mean's each array will multiple objects and if i take the sum of these objects "Active_Contracts_Count" that should be less than 100.

 

sample input:

[

{

"Org_Id" : "abc1234-efg1234",

"Org_Name" : "SAAS",

"Env_Id" : "env99-en32",

"Env_Name" : "SAAS-QA",

"Api_Id" : "13444",

"Active_Contracts_Count" : 18

},

{

"Org_Id" : "abc1234-efg1234",

"Org_Name" : "SAAS",

"Env_Id" : "4344-en32",

"Env_Name" : "SAAS-DEV",

"Api_Id" : "18829",

"Active_Contracts_Count" : 7

},

{

"Org_Id" : "abc1234-efg1234",

"Org_Name" : "SAAS",

"Env_Id" : "ss-e21-sd",

"Env_Name" : "SAAS-CICD",

"Api_Id" : "28943",

"Active_Contracts_Count" : 55

},

{

"Org_Id" : "abc1234-efg1234",

"Org_Name" : "SAAS",

"Env_Id" : "4344-en32",

"Env_Name" : "SAAS-DEV",

"Api_Id" : "35765",

"Active_Contracts_Count" : 8

},

{

"Org_Id" : "abc1234-efg1234",

"Org_Name" : "SAAS",

"Env_Id" : "4344-en32",

"Env_Name" : "SAAS-DEV",

"Api_Id" : "40349",

"Active_Contracts_Count" : 19

},

{

"Org_Id" : "abc1234-efg1234",

"Org_Name" : "SAAS",

"Env_Id" : "env99-en32",

"Env_Name" : "SAAS-QA",

"Api_Id" : "40382",

"Active_Contracts_Count" : 98

}

]

expected output

[

[

{

"Org_Id": "abc1234-efg1234",

"Org_Name": "SAAS",

"Env_Id": "env99-en32",

"Env_Name": "SAAS-QA",

"Api_Id": "13444",

"Active_Contracts_Count": 18

},

{

"Org_Id": "abc1234-efg1234",

"Org_Name": "SAAS",

"Env_Id": "4344-en32",

"Env_Name": "SAAS-DEV",

"Api_Id": "18829",

"Active_Contracts_Count": 7

},

{

"Org_Id": "abc1234-efg1234",

"Org_Name": "SAAS",

"Env_Id": "ss-e21-sd",

"Env_Name": "SAAS-CICD",

"Api_Id": "28943",

"Active_Contracts_Count": 55

},

{

"Org_Id": "abc1234-efg1234",

"Org_Name": "SAAS",

"Env_Id": "4344-en32",

"Env_Name": "SAAS-DEV",

"Api_Id": "35765",

"Active_Contracts_Count": 8

}

],

[

{

"Org_Id": "abc1234-efg1234",

"Org_Name": "SAAS",

"Env_Id": "env99-en32",

"Env_Name": "SAAS-QA",

"Api_Id": "40382",

"Active_Contracts_Count": 98

}

],

{

"Org_Id": "abc1234-efg1234",

"Org_Name": "SAAS",

"Env_Id": "4344-en32",

"Env_Name": "SAAS-DEV",

"Api_Id": "40349",

"Active_Contracts_Count": 19

}

]

 

#DataWeave 2​ 

2 个回答
  1. Anurag Sharma (Nagarro) Forum Ambassador
    2023年10月28日 04:28

    Hello @Rajat Raj​ 

     

    try this

    %dw 2.0

    output application/json

     

    fun sumOfLastArray(arr) =

    if ((arr != null) and (sizeOf(arr) > 0) and (typeOf(arr[-1]) ~= "Array"))

    ((arr[-1] reduce ((item, total = 0) -> total + item.Active_Contracts_Count)))

    else 0

     

    ---

    payload reduce (e, acc = []) ->

    if (isEmpty(acc) or ((sumOfLastArray(acc) + e.Active_Contracts_Count) > 100))

    (acc ++ [[e]])

    else acc[0 to -2] default [] ++ [(acc[-1] ++ [e])]

0/9000

Hi - I'm calling a web service url with HTTP Request component. However i have to generate the soap headers manually. Its working fine in postman. But now to make it work in dwl i'm facing transformation issue while concatenating xml paylod with soap headers.

 

payload:

<?xml version='1.0' encoding='UTF-8'?>

<man:submitDocument xmlns:man="http://www.t.com/ws/integration/toolkit/2011/05/management">

<Document xmlns:ns2="http://www.t.com/ws/integration/toolkit/2011/05" ns2:name="">

<Attributes>

<Attribute name="locale">en</Attribute>

<Attribute name="largegraph">true</Attribute>

<Attribute name="mode">T-XML</Attribute>

<Attribute name="version">http://www.t.com/ws/tee800/2009/01</Attribute>

</Attributes>

<Content>

<ExportQuery xmlns:ns1="http://www.t.com/ws/so800/2009/01" ns1:name="">

<quer:query xmlns:quer="http://www.t.com/ws/integration/query" alias="Query752BAE88-55BD-11E2-8B1D-CDAFDC756D0C" preventDuplicates="false" projectedClass="Application">

<quer:subQueries/>

<quer:projections>

<quer:projection alias="FirstName">

<quer:field path="Candidate,FirstName"/>

</quer:projection>

<quer:projection alias="MiddleName">

<quer:field path="Candidate,MiddleInitial"/>

</quer:projection>

<quer:projection alias="LastName">

<quer:field path="Candidate,LastName"/>

</quer:projection>

<quer:projection alias="IsSubmissionExpired">

<quer:field path="Applications,IsSubmissionExpired"/>

</quer:projection>

</quer:projections>

<quer:projectionFilterings/>

<quer:filterings>

<quer:filtering>

<quer:or>

<quer:equal>

<quer:field path="CSWLatestStep,Name"/>

<quer:string>Pre-Employment Process</quer:string>

</quer:equal>

<quer:equal>

<quer:field path="CSWLatestStep,Name"/>

<quer:string>Terminal Pre-Employment Process</quer:string>

</quer:equal>

</quer:or>

</quer:filtering>

<quer:filtering>

<quer:equal>

<quer:field path="CSWLatestStatus,Name"/>

<quer:string>Move to Hired</quer:string>

</quer:equal>

</quer:filtering>

<quer:filtering>

<quer:equal>

<quer:field path="Candidate,InternalApplication"/>

<quer:string>0</quer:string>

</quer:equal>

</quer:filtering>

</quer:filterings>

<quer:sortings/>

<quer:sortingFilterings/>

<quer:groupings/>

<quer:joinings/>

</quer:query>

</ExportQuery>

</Content>

</Document>

</man:submitDocument>

dwl

%dw 2.0

output application/xml

var username = "test"

var password = "test"

 

var soapPayload = read('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soap:mustUnderstand="1"><wsse:UsernameToken wsu:Id="Username"><wsse:Username>' ++ username as String ++ '</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' ++ password as String ++ '</wsse:Password></wsse:UsernameToken></wsse:Security><add:Action xmlns:add="http://www.w3.org/2005/03/addressing">http://www.t.com/ws/integration/toolkit/2005/07/action/export</add:Action><add:ReplyTo xmlns:add="http://www.w3.org/2005/03/addressing"><add:Address>http://www.t.com/ws/integration/toolkit/2005/07/addressing/queue</add:Address></add:ReplyTo><add:MessageID xmlns:add="http://www.w3.org/2005/03/addressing">RE</add:MessageID></soap:Header><soap:Body>' ++ payload ++ '</soap:Body></soap:Envelope>', "application/xml")

---

soapPayload

 

error:

You called the function '++' with these arguments:

1: String ("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soa...)

2: Object (do {ns man http://www.t.com/ws/integration/toolkit/2011/05/management---{man#...)

 

But it expects one of these combinations:

(Array, Array)

(Date, Time)

(Date, LocalTime)

(Date, TimeZone)

(LocalDateTime, TimeZone)

(LocalTime, Date)

(LocalTime, TimeZone)

(Object, Object)

(String, String)

(Time, Date)

(TimeZone, LocalDateTime)

(TimeZone, Date)

(TimeZone, LocalTime)

 

6| var soapPayload = read('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soap:mustUnderstand="1"><wsse:UsernameToken wsu:Id="Username"><wsse:Username>' ++ username as String ++ '</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' ++ password as String ++ '</wsse:Password></wsse:UsernameToken></wsse:Security><add:Action xmlns:add="http://www.w3.org/2005/03/addressing">http://www.t.com/ws/integration/toolkit/2005/07/action/export</add:Action><add:ReplyTo xmlns:add="http://www.w3.org/2005/03/addressing"><add:Address>http://www.t.com/ws/integration/toolkit/2005/07/addressing/queue</add:Address></add:ReplyTo><add:MessageID xmlns:add="http://www.w3.org/2005/03/addressing">RE</add:MessageID></soap:Header><soap:Body>' ++ payload ++ '</soap:Body></soap:Envelope>', "application/xml")

^^^^

Trace:

at main::++ (line: 6, column: 19)

at main::++ (line: 6, column: 392)

at main::++ (line: 6, column: 414)

can someone help me out pls.

 

#Mule 4​ #DataWeave 2​ #soap #WEB SERVICES​ 

3 个回答
  1. 2022年12月8日 02:00

    Below script will help you.

     

    %dw 2.0

    output application/xml

    var username = "test"

    var password = "test"

    var soapPayload = read('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soap:mustUnderstand="1"><wsse:UsernameToken wsu:Id="Username"><wsse:Username></wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"></wsse:Password></wsse:UsernameToken></wsse:Security><add:Action xmlns:add="http://www.w3.org/2005/03/addressing">http://www.t.com/ws/integration/toolkit/2005/07/action/export</add:Action><add:ReplyTo xmlns:add="http://www.w3.org/2005/03/addressing"><add:Address>http://www.t.com/ws/integration/toolkit/2005/07/addressing/queue</add:Address></add:ReplyTo><add:MessageID xmlns:add="http://www.w3.org/2005/03/addressing">RE</add:MessageID></soap:Header><soap:Body></soap:Body></soap:Envelope>', "application/xml")

    ---

    soapPayload update {

    case .Envelope.Header.Security.UsernameToken.Username -> username

    case .Envelope.Header.Security.UsernameToken.Password -> password

    case .Envelope.Body -> payload

    }

0/9000

I have below input payload, the expected output should be swapped

 

Input:

[{

"name" : "A",

"id" : 1

},

{

"name" : "B",

"id" : 2

}]

output:

[{

"A" : "name",

1 : "id"

},

{

"B" : "name",

2 : "id"

}]



#DataWeave 2​ #Mule 4​ 

6 个回答
0/9000

how to convert .csv file to .zip file in Mule4. Is there any custom connectors ? Can someone let me know if there are better solution.

 

Note: java code is not preferred.

 

#DataWeave 2​ #Mule 4​ #DataWeave 1​ 

8 个回答
  1. 2020年2月11日 19:08

    if multiple files, I will make use of Archive operation instead of Compress. For entries, you can follow below

     

    {(payload.data map(value, index) -> {

    (value.fileName) : value.payload write "application/csv"

    }

    )}

     

    where you want to write set of entries. Above command will result to

    {

    "test1.csv" : [<content>],

    "test2.csv" : [<content>]

    }

0/9000

For ex:

i have below data in gsheet:

 

oppID status date

789 active 20/12/24

123 active 20/12/24

456 inactive 21/12/24

 

Now if a user want to update oppid 123 as inactive. how to do that dynamically?

i don't want to fetch whole data from gsheet and check in which row the oppid is. Is there any search mechanism i can have to directly search in gsheet and update the row

 

#Mule 4​  #DataWeave 2​ #Connectors​ 

2 个回答
  1. Shekh Muenuddeen (NTTData) Forum Ambassador
    2024年9月25日 14:23

    As per me there are two ways to do it

    • Using MuleSoft DataWeave - download excel , load into DataWeave and update row then DataWeave output will excel file and upload excel file.

     

    Read - Excel file

    %dw 2.0

    var myInput = readUrl("classpath://ourBugs.xlsx", "application/xlsx")

    output application/json

    ---

    myInput."Data"

    Generate Excel file

    %dw 2.0

    output application/xlsx header=true

    ---

    {

    Data: payload

    }

    • Read excel file using java or other scripting language which MuleSoft Supports. Using this way it might be easy to update excel file.

    Example

    https://dzone.com/articles/how-to-color-excel-file-using-mulesoft-1

0/9000

Hi,

How to find the difference b/w 2 dates in mule4 in period format ( P1D ).

 

Input:

{

"lsr": "2023-11-03 16:35:45",

"now": "2023-11-05 05:43:49"

}

dwl

%dw 2.3

output application/json

 

var today = now() as DateTime as String {format: "yyyy-MM-dd HH:mm:ss"}

var lsr = "2023-11-03 16:35:45" as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss"}

 

---

{

lsr: lsr,

now: today,

delta: "?"

}

 

Appreciate your help on this.

 

#DataWeave 2​ 

3 个回答
  1. 2023年11月6日 15:12

    To achieve the desired output format, one possible approach is to utilize the 'between' function.

     

    %dw 2.3

    var today = now() as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss"}

    var lsr = "2023-11-03 16:35:45" as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss"}

    output application/json

    import between from dw::core::Periods

    ---

     

    {

    lsr: lsr,

    now: today,

    delta: between(today,lsr)

    }

    To achieve the desired output format, one possible approach is to utilize the 'between' function. %dw 2. 

    Hope it helps.

0/9000

Input payload :

{

"line_items":

[

{

"id": "12344",

"unit_amount": 5999,

" billing_period": 1,

"billing_period_unit": "year"

},

{

"id": "8907",

"unit_amount": 676879,

" billing_period": 1,

"billing_period_unit": "year"

}

]

}

 

Output payload:

{

"line_items":

[

{

"id": "12344",

"unit_amount": 59.99,

"billing_period": 1,

"billing_period_unit": "year"

},

{

"id": "8907",

"unit_amount": 6768.79,

"billing_period": 1,

"billing_period_unit": "year"

}

]

}

 

I don't want to have one-one mapping, is there a way to use update function and just have the mapping for unit_amount as i want to have transformation logic only unit_amount i.e unit_amoount/100. Rest of the fields should be mapped as it is.

 

#Mule 4​  #DataWeave 2​ 

3 个回答
  1. 2023年8月9日 10:29

    %dw 2.0

    output application/json

    ---

    {

    "line_items": payload.line_items map {

    "id": $.id,

    "unit_amount": $.unit_amount / 100,

    "billing_period": $.billing_period,

    "billing_period_unit": $.billing_period_unit

    }

    }

0/9000

Input json:

[

{

"SR#": "1",

"PropertyName": "shipToaddres",

"YES": "success",

"NO": "fail",

"NA": "fail"

},

{

"SR#": "2",

"PropertyName": "billToadress",

"YES": "fail",

"NO": "fail",

"NA": "success"

}

]

 

required output:

{

"shipToaddres": "success",

"billToadress": "success"

}

 

dwl:

%dw 2.0

output application/json

var a = {

    "shipToaddres": "YES",

    "billToadress": "NA"

}

 

fun mapper(k,b) = payload map ((item, index) -> if(item.PropertyName ~= k ) item.b else "") joinBy ""

 

---

a mapObject ((value, key, index) ->(key): mapper((key),value) )

 

II dont understand why item.b resolves to null

 

#DataWeave 2​ #Mule 4​ #transformation

2 个回答
  1. 2023年7月3日 06:48

    In case of more than two objects in payload, you can do it in below way as well if it is relevant to your use case.

     

    %dw 2.0

    output application/json

    var a = {

    "shipToaddres": "YES",

    "billToadress": "NA"

    }

     

    var payloadMapper = {(payload map(

    ($.PropertyName) : $

    ))}

    ---

    payloadMapper mapObject ((value, key, index) ->{

    (key): value[a[key]]

    } )

     

    In case of more than two objects in payload, you can do it in below way as well if it is relevant to your use case. %dw 2.

0/9000