Sample input 1
{
"data": [
{
"a": [
{
"id": 123
}
],
"a1": [],
"a3": [],
"a4": []
},
{
"b": [
{
"bid": 133
}
],
"b1": [],
"b2": []
},
{
"c": [],
"c1": [],
"d": []
}
]
}
sample input 2: (based on which will filter the sample input 1)
[
"d",
"b",
"b1",
"a4"
]
by comparing the values of both the inputs
Scenario: based on the object names present in 2 input need to filter out the objects from the payload 1.
Expected final output:
{
"data": [{
"a": [{
"id": 123
}],
"a1": [],
"a3": []
},
{
"b2": []
},
{
"c": [],
"c1": []
}]
}
sample code:
%dw 2.0
output application/json
---
payload.data map ((item, index) -> item - "d" - "b" - "b1" - "a4") //
Note: This sample is working but but the values should be taken dynamically from the 2 input
Any help would be appreciated. Thank you.
Try with this:
Payload is the sample input 1 that you have typed in.
%dw 2.0
output application/json
var filterList = [ "d", "b", "b1", "a4" ]
---
data: payload.data map {
($ -- filterList)
}
Related
Input:
{
"count": 3,
"employees":
[
{
"name":"appy",
"age":34
},
{
"name":"happy",
"age":38
},
{
"name":"cruise",
"age":36
}
]
}
Output:
[
{
"first":"appy",
"age":34
},
{
"first":"happy",
"age":38
},
{
"first":"cruise",
"age":36
}
]
This is my input i am trying to add "first" in "name" how can i do any suggestions i am using Map function here.
"first" in "name" place i am trying to use map function
Just map the elements:
%dw 2.0
output application/json
---
payload.employees map {
first: $.name,
age: $.age
}
Output:
[
{
"first": "appy",
"age": 34
},
{
"first": "happy",
"age": 38
},
{
"first": "cruise",
"age": 36
}
]
I'm looking for an output similar to this one below where i want to groupBy costomer and orderid.
Input:
[
{
"item": 621,
"orderid": "ON22",
"qty": 45.0,
"customer": "610",
"date": "1988-08-13"
},
{
"item": 63,
"orderid": "ON2234",
"qty": 7,
"customer": "813",
"date": "2001-08-13"
}
]
Desired output:
[
{
"customer":"813",
"data":[
{
"item":63,
"qty":7,
"orderid":"ON2234",
"date":"2001-08-13"
}
]
},
{
"customer":"610",
"data":[
{
"item": 621,
"qty": 45.0,
"orderid": "ON22",
"date": "1988-08-13"
}
]
}
]
You can simply map the default output of your groupBy result since your output does not require any additional logic.
%dw 2.0
output application/json
---
payload groupBy $.customer pluck ((customerOrders, customerId) -> {
customer: customerId as String,
data: customerOrders
})
How to write Dataweave transformation in Anytime Studio for given input and output of Json array.
Input:
{
"result": [{
"Labels": [{
"value": [{
"fieldName": "firstName",
"value": "John"
},
{
"fieldName": "lastName",
"value": "Doe"
},
{
"fieldName": "fullName",
"value": "John Doe"
}
]
}]
}]
}
Output:
{
"result": [{
"Labels": [{
"value": [{
"firstName": "John",
"lastName": "Doe",
"fullName": "John Doe"
}]
}]
}]
}
https://docs.mulesoft.com/dataweave/2.4/dw-core-functions-reduce Reduce function might be the one should be used
Thank you in advance
You can just use map to map all the arrays to required format. For the value part you can map the values as fieldName: value array and deconstruct them to an object by wrapping the array around parentheses
%dw 2.0
output application/json
---
{
result: payload.result map ((item) -> {
Labels: item.Labels map ((label) -> {
value: [
{
(label.value map ((field) ->
(field.fieldName): field.value
)) //wrap the array, i.e. lavel.value map ... in parentheses so that it will give you individual key pair.
}
]
})
})
}
You can try below if you are aware that the keyNames will not change:
%dw 2.0
output application/json
---
payload update {
case res at .result -> res map (res, resIndex) -> (res update {
case lbl at .Labels -> lbl map (lbl, lblIndex) -> (lbl update {
case val at .value -> [
(val reduce ((item, acc = {}) -> acc ++ {
(item.fieldName): (item.value)
}))
]
}
)
}
)
}
Here's 2 caveats and a solution. Your input and output files, both are not valid JSON.
Input file, in your "result" object, "Labels" need curly braces {} since they are objects. Key-value pairs should look like this {key:value} not like that key:value
Output file, inside your "value" arrays, key-value pairs need to have the curlies {key:value}
So here's a valid JSON version of your input
{
"result": [
{"Labels": [
{
"value": [
{"fieldName": "firstName","value": "John"},
{"fieldName": "lastName","value": "Doe"},
{"fieldName": "fullName","value": "John Doe"}
]
}
]},
{"Labels": [
{
"value": [
{"fieldName": "firstName","value": "John"}
]
}
]}
]}
Here's a solution
%dw 2.0
import keySet from dw::core::Objects
// this is "result"
var layer1key = keySet(payload)[0]
// this is "Labels" and grabs the first Labels, so assumes Labels doesn't change
var layer2 = payload[layer1key]
var layer2key = keySet(layer2[0])[0]
// this is "value"
var layer3 = layer2[layer2key]
var layer3key = keySet(layer3[0][0])[0]
// this is "fieldName" and "value"
var layer4 = layer3 map (x) -> x['value']
var data1 = ((layer1key) : layer4 map (x) -> {
(layer2key): x map (y) -> {
(layer3key): y map (z) -> {
(z['fieldName']):z['value']
}
}
})
output application/json
---
data1
And a valid JSON version of your output
{
"result": [
{
"Labels": [
{
"value": [
{
"firstName": "John"
},
{
"lastName": "Doe"
},
{
"fullName": "John Doe"
}
]
}
]
},
{
"Labels": [
{
"value": [
{
"firstName": "John"
}
]
}
]
}
]
}
How to join 3 arrays by index (lines, pckSlip and linesDt) and generate an arrays object by linesDt after that you have to generate a new field "totalCost" which is to add the "cost" field of all cost elements in the linesDt array, note the "number" field in the original object is preserved for all new elements spawned from the parent.
Input:
{
"lines":[
{
"requestedQuantity":1,
"pendingQuantity":0
},
{
"requestedQuantity":2,
"pendingQuantity":0
}
],
"number":"98",
"pckSlip":[
{
"trackingNumber":"10534",
"boxesNum":1
},
{
"trackingNumber":"00049",
"boxesNum":1
}
],
"linesDt":[
{
"number":"5678",
"cost":7.7
},
{
"number":"1234",
"cost":7.3
}
]
}
Ouput:
[
{
"number":"5678",
"cost":7.7,
"requestedQuantity":1,
"pendingQuantity":0,
"trackingNumber":"10534",
"boxesNum":1,
"totalCost":15,
"order":"98"
},
{
"number":"1234",
"cost":7.3,
"requestedQuantity":2,
"pendingQuantity":0,
"trackingNumber":"00049",
"boxesNum":1,
"totalCost":15,
"order":"98"
}
]
NOTE: We generate 2 new elements because they are the total of indices found in "linesDt" within an array of elements.
Any help would be appreciated. Thank you.
Mapping each element of lines gives you the index to use in the other arrays. The ++ operator can be used to concatenate objects all the objects together. The calculated fields are added just as another object.
%dw 2.0
output application/json
var totalCost = sum(payload.linesDt.*cost)
---
payload.lines map (
$
++ payload.pckSlip[$$]
++ payload.linesDt[$$]
++ {totalCost: totalCost, order: payload.number}
)
Output:
[
{
"requestedQuantity": 1,
"pendingQuantity": 0,
"trackingNumber": "10534",
"boxesNum": 1,
"number": "5678",
"cost": 7.7,
"totalCost": 15.0,
"order": "98"
},
{
"requestedQuantity": 2,
"pendingQuantity": 0,
"trackingNumber": "00049",
"boxesNum": 1,
"number": "1234",
"cost": 7.3,
"totalCost": 15.0,
"order": "98"
}
]
Assuming that the size of each of the arrays is going to be the same.
Script
%dw 2.0
output application/json
---
1 to sizeOf(payload.lines) map {
(payload.linesDt[($$)] ++ payload.lines[($$)] ++ payload.pckSlip[($$)] ++ ("totalCost": sum(payload.linesDt..cost) as String) ++ ("order": payload.number))
}
Output
[
{
"number": "5678",
"cost": 7.7,
"requestedQuantity": 1,
"pendingQuantity": 0,
"trackingNumber": "10534",
"boxesNum": 1,
"totalCost": "15",
"order": "98"
},
{
"number": "1234",
"cost": 7.3,
"requestedQuantity": 2,
"pendingQuantity": 0,
"trackingNumber": "00049",
"boxesNum": 1,
"totalCost": "15",
"order": "98"
}
]
I am trying to transform an xml based on groupBy in dataweave, however I also need to remove few json attributes from output.
Input json:
[ {
"m": {
"a": "a",
"b": "b"
},
"tag1": "A",
"tag2": "v1",
"tag3": "v1" }, {
"m": {
"a": "a",
"b": "b"
},
"tag1": "A",
"tag2": "v2",
"tag3": "v2" }, {
"m": {
"a": "a",
"b": "b"
},
"tag1": "C",
"tag2": "v3",
"tag3": "v3" } ]
Output json
**{
"A": [
{
"tag2": "v1",
"tag3": "v1"
},
{
"tag2": "v2",
"tag3": "v2"
}
],
"C": {
"tag2": "v3",
"tag3": "v3"
}
}**
I have tried following transformation (Mule 3.9) however the could not remove the extra attributes in json.
payload groupBy (item) -> item.tag1
Appreciate any suggestion on this and possibly explain how this can be achieved.
The way to iterate over an object is using mapObject and then you can filter the objects to remove the elements that are not wanted
{a: [1,2,3], b: [2,3]} mapObject ((value,key) ->
{
(key): value filter ((value, index) -> value > 2)
}
)
This will output
{
"a": [
3
],
"b": [
3
]
}