Hello I have a json file as payload and strugling to do Transformation to lower case of elements and they values.
{
"Name" : "John".
"e-mails" : ['Email1#mail.com','email2#Gmail.com']
}
if its no array in values then this one works fine like where
but how to deal with arrays?
expected output:
{
"name" : "john".
"e-mails" : ['email1#mail.com','email2#gmail.com']
}
any advice?
You need to use a recursive function to cover for the other types.
%dw 1.0
%output application/json
%function lowerAll(x)
x match {
:object -> $ mapObject {
(lower $$): lowerAll($) // assumes all keys are strings
},
:array -> $ map lowerAll($),
:string -> lower $,
default -> $
}
---
lowerAll(payload)
Input:
{
"Name" : "John",
"e-mails" : ["E1mail1#mail.com","email2#Gmail.com"]
}
Output:
{
"name": "john",
"e-mails": [
"e1mail1#mail.com",
"email2#gmail.com"
]
}
Related
I have the below dynamic response coming from third party API, now I need to transform only the particular JSON object ("MyValues") into an array.
The payload here is sample is is very large .
Current Output:
{
"Body": {
"Status": "200",
"Result": {
"MyValues":{
"Name":"ABC TEST",
"Phone":"1234"
}
}
}
}
Expected Output:
{
"Body": {
"Status": "200",
"Result": {
"MyValues":[{
"Name":"ABC TEST",
"Phone":"1234"
}]
}
}
}
You can use pattern matching based on the type received, array or object. I created a recursive function to find the instances of a key name and perform the change in a generic way.
Example:
%dw 1.0
%output application/json
%function convertToSingleArray(x, key)
x match {
// OPTIONAL :array -> x map convertToSingleArray($, key),
:object -> x mapObject {($$): [$] when ( (($$ as :string) == key) and ((typeOf $) as :string == ":object")) otherwise convertToSingleArray($, key)
},
default -> x
}
---
convertToSingleArray(payload, "MyValues")
I have such input:
{
Abc: "1",
BcD: "2",
...
klm: "3",
ZXC: "4"
}
I want to get output after transform like this:
{
abc: "1",
bcD: "2",
...
klm: "3",
zXC: "4"
}
How I can do that?
Have been tried like that:
%dw 1.0
%output application/json
---
{
($$) replace /^([A-Z])/ with lower $$[1] : $
}
but getting error:
There is no variable named '$$'
Try with this:
Input
{
"Abc": "1",
"BcD": "2",
"klm": "3",
"ZXC": "4"
}
Script
%dw 1.0
%input payload application/json
%output application/json
---
payload mapObject {
(lower ($$)[0] ++ (($$)[1 to -1])):$
}
Output
{
"abc": "1",
"bcD": "2",
"klm": "3",
"zXC": "4"
}
You have to use mapObject to change the keys. Note that $$ only makes sense in the context of some operators. You need to put the entire key expression between parenthesis. I used pattern matching instead.
Example:
%dw 1.0
%output application/java
%function lowerFirst(s)
s[0] match {
'A' -> 'a' ++ s[1..-1],
'B' -> 'b' ++ s[1..-1],
// add other letters mapping
default -> s
}
---
payload mapObject (lowerFirst($$)): $
Note that you need to complete the letters mapping in the function.
I get a payload as input in the message transform component. It is an Array with Objcts:
[
{
"enterprise": "Samsung",
"description": "This is the Samsung enterprise",
},
{
"enterprise": "Apple",
"description": "This is the Apple enterprise ",
}
]
I have a variable that replaces the description and the output that I want is:
[
{
"enterprise": "Samsung",
"description": "This is the var value",
},
{
"enterprise": "Apple",
"description": "This is the var value",
}
]
I tried to use:
%dw 2.0
output application/java
---
payload map ((item, index) -> {
description: vars.descriptionValue
})
But it returns:
[
{
"description": "This is the var value",
},
{
"description": "This is the var value",
}
]
Is possible to replace only the description value keeping the rest of the fields? Avoiding adding the other fields in the mapping.
There are many ways to do this.
One way to do it is to first remove the original description field and then add the new one
%dw 2.0
output application/java
---
payload map ((item, index) ->
item - "description" ++ {description: vars.descriptionValue}
)
Otherwise you can use mapObject to iterate over the key-value pairs of each object and with pattern matching add a case for when the key is description.
I prefer this second way of doing it when I want to do many replacements.
%dw 2.0
output application/java
fun process(obj: Object) = obj mapObject ((value, key) -> {
(key): key match {
case "description" -> vars.descriptionValue
else -> value
}
})
---
payload map ((item, index) ->
process(item)
)
Input:-
[
{
"appName": "helloworld-1"
},
{
"appName": "helloworld-2"
}
]
Expected Output
{
"appList": [ "helloworld-1" , "helloworld-2" ]
}
Can anyone guide me for data weave script for this ?
According to the DataWeave Documentation, you can apply the multi value selector. Multi value selector returns an array with all the values whose key matches the expression.
%dw 1.0
%output application/json
---
{
appList : payload.*appName
}
I have a list of maps (listOfMapsObject) like below
[
{
"Id" : "1234",
"Value" : "Text1"
},
{
"Id" : "1235",
"Value" : "Text2"
}
]
I would like access "Value" field for a given Id in dataweave script.
For example: For Id = 1234, Text1 should be returned.
%dw 1.0
%output application/json
%var listOfMapsObject = flowVars.listOfMaps
---
payload map {
"key" : $.key,
"data" : lookup Value field in listOfMapsObject by given key
}
Approch suggested by #'sulthony h' is fine but it will end up in performance issue if you large number of data in pyload and listOfMapsObject. As filter is used , for each record of payload script will loop for all the entries in flowVars.listOfMaps.
Following will work fine and map key value only once.
%dw 1.0
%output application/json
%var dataLookup = {(flowVars.listOfMaps map {
($.Id): $.Value
})}
---
payload map {
key : $.key,
data : dataLookup[$.key]
}
Output-
[
{
"key": "1234",
"data": "Text1"
},
{
"key": "1235",
"data": "Text2"
}
]
Where Payload -
[
{
"key" : "1234"
},
{
"key" : "1235"
}
]
And -
[
{
"Id" : "1234",
"Value" : "Text1"
},
{
"Id" : "1235",
"Value" : "Text2"
}
]
Hope this helps.
I create my own object with the slightly similar object and successfully access "value" field with the following DataWeave expression:
%dw 1.0
%output application/json
%var listOfMapsObject = flowVars.listOfMaps
---
payload map using(data = $) {
"key" : data.key,
"data" : (listOfMapsObject filter $.id == data.key).value reduce ($$ ++ $)
}
You can modify it with your own object, e.g.: replace the "id" with "Id". Test and evaluate the result by using filter, flatten, reduce, etc.