Reputation: 11
I have an input of the following payload for a POST method:
{
"order": {
"Column_X": "X",
"Column_Y": "Y",
"Column_Z": "Z",
"Column_W" : {
"div_1": "some text",
"div_2": true,
"div_3": 2
}
},
"mapper": {
"A": "<order.Column_X>",
"B": "<order.Column_Z>",
"C": "Fields or text not mapped to order",
"C1": "status",
"D": {
"D1": "<order.Column_W.div_1>",
"D2": "<order.Column_W.div_2>",
"D3": "<order.Column_W.div_3>",
"D4": "<order.Column_W.div_4>"
}
}
}
Here is the expected output mapping with the order object above:
{
"A":"X",
"B":"Z",
"C":"Fields or text not mapped to order",
"C1":"status",
"D":{
"D1":"some text",
"D2":true,
"D3":2,
"D4":null
}
}
How would I approach this to solve it?
Upvotes: 1
Views: 213
Reputation: 3262
To make it fully dynamic you need two things.
%dw 2.0
output application/json
fun isPath(value) = (value is String) and (value matches "<.+>")
fun getFromPath(json, pathString) =
pathString splitBy '.'
reduce ((path, valueAtPath = json) -> valueAtPath[path])
fun buildUsingMapper(mapper, mappingValues) =
mapper mapObject ((value, key) -> {
(key): if(isPath(value)) mappingValues getFromPath value[1 to -2] // eliminate the first "<" and the ending ">".
else if(value is Object) buildUsingMapper(value, mappingValues)
else value
})
---
buildUsingMapper(payload.mapper, payload - "mapper") // you can also pass the full payload instead of payload - "mapper". I did it here to show that the second parameter does not necessarily need the mapper object within.
I have assumed there are no arrays that will have these dynamic path in mapper
, Or the dynamic paths will include accessing an array element dynamically.
Upvotes: 1
Reputation: 4303
This is bit crude approach though hopefully it gives you an idea to query the "order" object a bit dyamically.
%dw 2.0
output application/json
fun returnFromOrder(val : Array) = {
a: if (sizeOf(val) ==2) payload."$(val[0])"."$(val[1])" else payload."$(val[0])"."$(val[1])"."$(val[2])"
}
---
{
A: returnFromOrder(( payload.mapper.A replace "<" with "" replace ">" with "" splitBy ".")).a,
B: returnFromOrder(( payload.mapper.B replace "<" with "" replace ">" with "" splitBy ".")).a,
C: if (returnFromOrder(( payload.mapper.C replace "<" with "" replace ">" with "" splitBy ".")).a == null) payload.mapper.C else "",
C1: if (returnFromOrder(( payload.mapper.C1 replace "<" with "" replace ">" with "" splitBy ".")).a == null) payload.mapper.C1 else "",
D: {
"D1" : returnFromOrder(( payload.mapper.D.D1 replace "<" with "" replace ">" with "" splitBy ".")).a,
"D2" : returnFromOrder(( payload.mapper.D.D2 replace "<" with "" replace ">" with "" splitBy ".")).a,
"D3" : returnFromOrder(( payload.mapper.D.D3 replace "<" with "" replace ">" with "" splitBy ".")).a,
"D4" : returnFromOrder(( payload.mapper.D.D4 replace "<" with "" replace ">" with "" splitBy ".")).a
}
}
Upvotes: 1
Reputation: 467
I hope this is what you are looking for.
%dw 2.0
output application/json
---
{
A: payload.order.Column_X,
B: payload.order.Column_Z,
C: payload.mapper.C,
C: payload.mapper.C1,
D: {
D1: payload.order.Column_W.div_1,
D2: payload.order.Column_W.div_2,
D3: payload.order.Column_W.div_3,
D4: payload.order.Column_W.div_4
}
}
Output:
{
"A": "X",
"B": "Z",
"C": "Fields or text not mapped to order",
"C": "status",
"D": {
"D1": "some text",
"D2": true,
"D3": 2,
"D4": null
}
}
Upvotes: -1