Need to change csv header from camel case to snake case in dataweave - mule

I am working on a pipeline to dynamically dump all columns from the salesforce object to the S3 bucket.
I get all columns for a salesforce object using describe object API. I store all columns into a variable and then create a big SOQL query out of it and submit a bulk query job v2.
Now, this is the main problem. The Column name I am getting from the salesforce connector is in camelCase
[{
"Id": 123,
"FirstName": "Manual",
"MasterRecordId__c" :"abc"
},
{
"Id": 456,
"FirstName": "John",
"MasterRecordId__c" :"def"
}]
But I want column names to be in snake case
[{
"Id": 123,
"first_name": "Manual",
"master_record_id__c":"abc"
},
{
"Id": 456,
"first_name": "john",
"master_record_id__c":"def"
}]
I understand mulesoft has an underscore function to do the same thing, but I am not able to apply any function at "key" level.
Any lead would be really helpful. Please let me know for any questions.

You just have to use mapObject along with the underscore function
%dw 2.0
import underscore from dw::core::Strings
output application/json
---
payload map ((item) ->
item mapObject ((value, key) -> {
(underscore(key)): value
})
)

In case you want Id field to remain as it is, give a try like below:
%dw 2.0
import * from dw::core::Strings
output application/json
---
payload map ($ mapObject ((value, key, index) -> if (capitalize(key as String) == key as String)
{
(key): value
}
else
{
(underscore(key)): value
}))

Related

how properly to add and remove elements from payload? (or replace on condition)

So I have payload that have old type of attributes, and I want to migrate them to be as new ones that all the rest logic is using. So before do validation I want modify it a bit.
Currently I manage to add and remove in separate transforms, but should it be possible to do in one go?
example payload:
{
"country": "Country",
"town": "Town",
"district": "Dist",
"owner": "Owner"
}
and output should be:
{
"country": "Country",
"city": "Town",
"area": "Dist",
"owner": "Owner"
}
so I add transform:
%dw 1.0
%output application/json
---
payload ++ {city: payload.town}
when
payload.town != null
otherwise
payload ++ {area: payload.distrinct}
when
payload.distrinct != null
otherwise
payload
I want to check if payload have no null values in town key and add new key city with town key value, and same check if distrinct is not null then add its value as area key. However its happening only for city (I know it will be added at the bottom, but order is not a problem in my case) however keys may not present (it may no town, or may no distrinct or may no both)
And on next transform:
%dw 1.0
%output application/json
---
payload -- {town: payload.town}
when
payload.town != null
otherwise
payload
I try to check if keys exist then delete old ones, but no luck on such :(
Any help?
That's too complicated. Instead of adding and removing keys you can just use mapObject to transform each key. Then it becomes trivial to parametrize the transformation. Also using default is simpler than when...otherwise when a value is null.
%dw 1.0
%output application/json
%var keyMap={ town: "city", district: "area" }
%function replaceKey(keyName) (keyMap[keyName] default keyName)
---
payload mapObject ( (replaceKey($$)) : $ )

Splitting concatenated values in table column using Dataweave

I have sample data in my Salesforce table as given below:
I am on Mule 3.9 and running dataweave 1.0.
I need to use dataweave to read the above data (from a Salesforce table) and transform it into a JSON as given below:
[
{"Id": "634594cc","Name": "Alpha","List": "AB01"},
{"Id": "634594cc","Name": "Alpha","List": "AB02"},
{"Id": "634594cc","Name": "Alpha","List": "AB03"},
{"Id": "5d839e9c","Name": "Bravo","List": "CD01"},
{"Id": "5d839e9c","Name": "Bravo","List": "CD02"},
{"Id": "3a5f34d3","Name": "Charlie","List": null}
]
As you can see above, the "List" column is what I need to split as separate arrays in the final JSON. It has data with semicolon at the begin, in between and in the end.
Thanks in advance for your help.
I took the liberty to create a couple of sample data based upon the SS (BTW, its best not to use SS) :).
Try this:
%dw 1.0
%output application/dw
%var data = [
{
id: "ABC123",
name: "A",
list: ";AB1;AB2;AB3;"
},
{
id: "ZXY321",
name: "B",
list: null
}
]
---
data reduce (e,result=[]) -> (
result ++ using (
list = e.list default "" splitBy /;/ filter ($ != ""),
sharedFields = {
Id: e.id,
Name: e.name
}
) (
using (
flist = list when ((sizeOf list) > 0) otherwise [null]
) (
flist map {
(sharedFields),
List: $
}
)
)
)

Filter Payload with Dataweave for a Set Variable component

For the Mulesoft 4.2 Set Variable component, I want to assign a simple String value pulled from a single specific record from an incoming JSON payload.
In my case, taking just the value from the 'country' field from below example:
[
{
"salesID": "4404Bob Builder210032011-02-18T15:52:21+0.00",
"id": "4404",
"firstName": "Bob",
"lastName": "Builder",
"address": "181 Construction Road, Adelaide, NSW",
"postal": "21003",
"country": "New Zealand",
"creationDate": "2011-02-18T15:52:21+0.00",
"accountType": "personal",
"miles": 17469
}
]
A non-dynamic way to do this is like:
payload[0].country
I believe the best way to do this is with the filter function. The below option gives me the entire object, but I just want the country field.
payload filter ($.id == "4404")
Map function seems to be overkill for this since I only want the value, itself. I must be missing the syntax to get at the country field.
I did some more investigating, and this solution got me close enough to what I wanted:
https://stackoverflow.com/a/43488566/11995149
For my code example using filter, I had to surround the whole expression in parenthesis, and then I can access the field with a dot reference,
but below code gives String value as a single record within an array:
(payload filter ($.id == "4404")).country
[
"New Zealand"
]
In my case, I know that just one result will be returned from the filtered payload, so I could get just the String value with:
(payload filter ($.id == "4404"))[0].country
enter image description here
Can you try any of below options:
1) (payload groupBy ((item, index) -> item.id))["4404"][0].country
OR
2) (payload map ((item, index) -> if(item.id == "4404") item.country else ""))[0]
Thanks,
Ashish

Merge two JSON outputs into one with Dataweave

I currently have two JSON files with data.
The first has a list of actions by a user. However, the userID is a UID string instead of their user name.
The second is a JSON list of UID strings with their corresponding user names. I stored this JSON in a Flow Variable named UserPayload
I'd like to merge the user name into the first list so that my output has actual users names instead of the UID.
Here is what I had so far...but the userId part doesn't work since my flowVar is an list.
%dw 1.0
%output application/csv
---
payload map ((payload01, indexOfPayload01) -> {
user: payload.01.userId,
actions: payload01.action,
FileName: payload01.payload.properties.name default "N/A",
userId: flowVars.UserPayload.objectName
})
Any help would be appreciated. Thanks!
you can access the right object from the UserPayload flowVar by either filtering the content of UserPayload based on userId or creating a associative array from UserPayload.
filtering UserPayload
%dw 1.0
%output application/json
---
payload map (action, index) -> {
user: action.user,
action: action.action,
FileName: action.FileName,
userId: (flowVars.UserPayload filter $.objectId == action.user)[0].objectName
}
associative array
%dw 1.0
%output application/json
%var users = flowVars.UserPayload groupBy $.objectId
---
payload map (action, index) -> {
user: action.user,
action: action.action,
FileName: action.FileName,
userId: users[action.user][0].objectName
}
for my examples i used following sample data derived from your question.
flowVars.UserPayload
[{
"objectId": "0000-0000-0001",
"objectName": "first user"
}, {
"objectId": "0000-0000-0002",
"objectName": "second user"
}]
payload
[{
"user": "0000-0000-0001",
"action": "some crazy action",
"FileName": "some-crazy-action.mov"
}]

How to get unnamed object instead of an array in case of only one result in Dataweave

We have a transformation with Dataweave which processes a list of objects. We get a json response like that:
{"hotels": [{
"name": "Hotel Oslo",
"propertyCode": "12345",
"currency": "NOK"
},
{
"name": "Hotel Stockholm",
"propertyCode": "12346",
"currency": "SEK"
}]}
However, in the case of only 1 response, we want to have the following response:
{"name": "Hotel Stockholm",
"propertyCode": "12346",
"currency": "SEK"}
We are generating the response like this:
{
hotels: payload.rows map ((row , indexOfRow) -> {
name: row.content.companyName.content,
propertyCode: row.content.propertyCode.content,
currency: row.content.currencyCode.content
})}
What should we put as a condition so that we do not get an array in case of 1 result?
Try this:
%dw 1.0
%output application/json
%function makeHotel(row) {
name: row.name,
propertyCode: row.propertyCode,
currency: row.currency
}
---
{
hotels: payload.rows map ((row , indexOfRow) -> makeHotel(row))
} when ((sizeOf payload.rows) != 1)
otherwise makeHotel(payload.rows[0])
It will give you an empty array on empty input, the simple object for one input and the structure with array when you have more than one input row.
(For test purposes, with a slightly differnt input structure, but the general solution should be clear.)