Skip to main content
Groupe

MuleSoft Training: DataWeave 2.0

Diving deep into DataWeave with DEX480? Join fellow developers and architects here to discuss complex transformations, functions, modules, and best practices. Share your challenges and solutions!

I am on Section 2.3 of Dataweave Learn. 

 

Here is the output: 

{ 

  "string": "String", 

  "number": "Number", 

  "boolean": "Boolean" 

} 

 

How can I modify this script to get the output above: 

%dw 2.0 

output json 

--- 

{ 

  "string": "hello", 

  "number": 1, 

  "boolean": true 

}    

6 réponses
  1. Forum Ambassador Khyati Mehta (InfinySkills)
    21 août 2025, 13:02

    You need to replace the actual values with their data type names as strings. I think this should work:

    %dw 2.0

    output json

    ---

    {

    string: "String",

    number: "Number",

    boolean: "Boolean"

    }

0/9000

this is my payload

[{

"name": "Zahid"

},

{

"name": "khan"

},

{

"name": "MuleSoft"

}

]

I need to change the key name to user1 user 2 user 3 and so on, so my out should be like

[

{

"user1": "Zahid"

},

{

"user2": "khan"

},

{

"user3": "MuleSoft"

}

]

Note: I am new and learning Dataweave concepts. my friend asked me to join this group and ask any questions for Dataweave. Please help

6 réponses
  1. Forum Ambassador Anurag Sharma (Nagarro)
    2 mai 2022, 05:33

    Hello @Zakir Kh​ ,

     

    Welcome to the Helper forum. You can solve this question in two ways

     

    answer-1

    %dw 2.0

    output application/json

    ---

    payload map {

    ('user'++ $$+1) : $.name

    }

    answer-1

    %dw 2.0

    output application/json

    ---

    payload map ((item, index) -> {

    ('user' ++ (index+1)) : item.name

    })

    I have used the map function to solve your question, for more knowledge on Map go through this documentation https://docs.mulesoft.com/dataweave/2.4/dw-core-functions-map

0/9000

I have this input payload

{

"order": [{

"name": "mule",

"country": "USA",

"id": "mule12"

},

{

"name": "soft",

"country": "brazil",

"id": "mule12"

},

{

"name": "tim",

"country": "java",

"id": "tim12"

},

{

"name": "los",

"country": "angelas",

"id": "tim12"

}

]

}

required output is

{

"mule12": [

{

"name": "mule",

"country": "USA"

},

{

"name": "soft",

"country": "brazil"

}

],

"tim12": [

{

"name": "tim",

"country": "java"

},

{

"name": "los",

"country": "angelas"

}

]

}

I have tried It using group By but I am getting the Id field and I want to remove it. Thanks In advance

4 réponses
  1. Forum Ambassador Anurag Sharma (Nagarro)
    4 mai 2022, 04:27

    Hello @Zakir Kh​ ,

     

    you can try this, I have used groupBy and then removed the "id" field

     

    %dw 2.0

    output application/json

    var test = payload.order groupBy ((item, index) -> item.id)

    ---

    test mapObject ((value, key, index) -> (key): value map $-"id" )

    Hello @Zakir Kh​ , you can try this, I have used groupBy and then removed the item." style="display: block;" />

0/9000

Hi All,

 

I tried this in Dataweave 1.0 and it works perfectly fine, however the same gives me error in Dataweave 2.0. Can some one please help me achieve the same output in 2.0

Input :

%dw 1.0

%output application/json

---

{

([1,2] map ({

test: 1

}) ++

({type : "filter"}))

}

 

Output:

{

 "test": 1,

 "test": 1,

 "type": "filter"

}

 

Thanks :)

4 réponses
  1. 2 mai 2020, 08:20

    Yes, as written, it was trying to concatenate an array with an object. Concatenate only works with like data types, so you have two options:

     

    Changing the () to [] on the last object produces an array of three objects, and the constructor/evaluation notation {( )} converts that to an object with three key:value pairs.

     

    Array concatenation:

    { ( [1,2] map ( {test: 1} ) ++ [ {"type" : "filter"} ] ) }

     

    Moving the closing )} immediately after the first object produces an object with two key:value pairs, which is then concatenated with the second object.

     

    Object concatenation:

    { ( [1,2] map ( {test:1} ) ) } ++ ( {"type": "filter"} )

     

    Incidentally, you can remove the () from around all objects in both examples. You only need the {( and )}.

0/9000

Hi everyone,

 

In Anypoint Studio, i would like to put a value as a key JSON. by default, i couldn't map the key.

input:

{

"total_rows": 7,

"offset": 0,

"rows": [{

"id": "0dd206faa4e454bbbf8133ab8c0a87bc",

"key": "0dd206faa4e454bbbf8133ab8c0a87bc",

"doc": {

"_id": "0dd206faa4e454bbbf8133ab8c0a87bc",

"_rev": "1-c140584e7b8a7841b6a1b85baa77c82d",

"recordType": "product",

"productID": "CK234324",

"productName": "Branded Air Fly High Quality Sneakers",

"category": "Enterprise",

"SKU": "CKS100",

"price": 100,

"inventory": 35868

}

}]

}

 

expected Output:

[{

"CK234324": {

"_id": "0dd206faa4e454bbbf8133ab8c0a87bc",

"_rev": "1-c140584e7b8a7841b6a1b85baa77c82d",

"recordType": "product",

"productID": "CK234324",

"productName": "Branded Air Fly High Quality Sneakers",

"category": "Enterprise",

"SKU": "CKS100",

"price": 100,

"inventory": 35868

}

}]

Here an example if it's not clear.

 

Thanks All.

Djoly

8 réponses
  1. 26 avr. 2020, 17:12

    Then you can use the script as under:

    %dw 2.0

    output application/json

    import * from dw::core::Objects

    ---

    [(payload.rows map {

    ($..productID[0]) : $.doc

    }) reduce (it, ac = {}) -> (ac mergeWith it)]

0/9000

Hi can anyone advise on what the issue is I'm getting when doing this walkthough?

 

Dataweave is

 

%dw 2.0

import dw::core::Strings

import modules::Util

output application/xml

var remapObject = {

Label: {key: (k,v) -> upper(k), value: ((k,v) -> Strings::capitalize(v)) }

}

---

//vars.transactions[0].customerRef

//vars.accounts[vars.transactions[0].customerRef as String]

do {

var joinData =

{transactions:

vars.transactions mapObject((value,key,index) ->

log("\n\nNext step $(index), key is $(key), and the customerRef is $(value.customerRef as String) \n", {}) ++

{ (Strings::dasherize(key ++ "Number" ++ index)): (value ++

vars.accounts[value.customerRef as String] as Object)

}

)

}

---

Util::pascalize(joinData) modules::APDevDataWeave::remap remapObject

}

And I get errors in dataweave stating

 

Unable to resolve reference of Strings::trim.

Unable to resolve reference of Strings::upper.

Unable to resolve reference of Strings::upper.

Unable to resolve reference of Strings::upper.

 

I get the same if I import the solution as well.

 

This is from the mule443 course.

2 réponses
  1. 12 févr. 2019, 05:59

    i've just run into the same problem.

     

    running Anypoint Studio 7.3.2.201901012049

    Mule Server Runtime 4.1.4 EE 7.3.2.201902011858

     

    however when i use an older version it works. i suspect the recent upgrade has broken something

     

    Anypoint Studio 7.2.3.201809241235

    Mule Server Runtime 4.1.2 EE 7.1.2.201807312059

0/9000

Hi, How can I convert date and time in the format below.

Mon Jan 28 00:01:02 EST 2021

Please help.

Thanks in advance.

5 réponses
0/9000

I have an Mule integration, which gets the payload from one salesforce org and send that to another salesforce org using MuleSoft. We are seeing the issue with when the incoming payload has special (e.g., Accents, curly quote, etc.). We are able to strip out Accents by using StringUtils, but not sure how to handle the curly single and double quote. I had tried using the replace function to update the field data. It seems to be working when I tested out in my local, but while deploying, it is not working. Can someone help me point to the right direction, please? Thank you!

0/9000

HI All,

 

i have an object store variable(key as Id, value as Boolean). Now i want to iterate the store variable and prepare Json array out of it. Please guide me.

 

My end goal is to hit the salesforce with this json array(single DML for bulk records instead record by record)

1 réponse
  1. Forum Ambassador Anurag Sharma (Nagarro)
    29 janv. 2024, 13:02

    To achieve your goal of iterating over an Object Store variable in MuleSoft and preparing a JSON array from it, you will typically follow these steps:

     

    First, you need to retrieve the contents of your Object Store. Depending on how your Mule application is set up, this might involve using the Object Store connector or accessing the Object Store programmatically.

     

    Once you have the contents of the Object Store, you can iterate over them. In DataWeave, you can use the

    map function to iterate over each key-value pair in your data.

     

    As you iterate over each item, you'll transform each key-value pair into a JSON object and then collect these objects into an array.

     

    Finally, you'll use this JSON array to make a bulk API call to Salesforce. Make sure that the JSON structure you create matches what the Salesforce API expects for bulk operations.

0/9000