Reputation: 27
Need to convert multi-dimensional array (json) into single dimensional array by repeating parent attribute with child attribute. Condition is that parent may have child or may not have child. There is 100s of attribute that needs to map so would be great if I can map every attribute without defining individual attribute names (if feasible).
It would be great if this problem could be solved by only using .dwl Original payload:
[
{
"id": "1",
"parentAttribute1": "parent1-1",
"parentAttribute2": "parent1-2",
"parentAttribute3": "parent1-3",
"child": [
{
"childAttribute1": "inner1-1-1",
"childAttribute2": "inner1-1-2"
},
{
"childAttribute1": "inner1-2-1",
"childAttribute2": "inner1-2-2"
},
{
"childAttribute1": "inner1-3-1",
"childAttribute2": "inner1-3-2"
}
]
},
{
"id": "2",
"parentAttribute1": "parent2-1",
"parentAttribute2": "parent2-2",
"parentAttribute3": "parent2-3",
"child": [
{
"childAttribute1": "inner2-1-1",
"childAttribute2": "inner2-1-2"
}
]
},
{
"id": "3",
"parentAttribute1": "parent3-1",
"parentAttribute2": "parent3-2",
"parentAttribute3": "parent3-3"
}
]
Expected payload after translation - Scenario 1 - All attributes
[
{
"id": "1",
"parentAttribute1": "parent1-1",
"parentAttribute2": "parent1-2",
"parentAttribute3": "parent1-3",
"childAttribute1": "inner1-1-1",
"childAttribute2": "inner1-1-2"
},
{
"id": "1",
"parentAttribute1": "parent1-1",
"parentAttribute2": "parent1-2",
"parentAttribute3": "parent1-3",
"childAttribute1": "inner1-2-1",
"childAttribute2": "inner1-2-2"
},
{
"id": "1",
"parentAttribute1": "parent1-1",
"parentAttribute2": "parent1-2",
"parentAttribute3": "parent1-3",
"childAttribute1": "inner1-3-1",
"childAttribute2": "inner1-3-2"
},
{
"id": "2",
"parentAttribute1": "parent2-1",
"parentAttribute2": "parent2-2",
"parentAttribute3": "parent2-3",
"childAttribute1": "inner2-1-1",
"childAttribute2": "inner2-1-2"
},
{
"id": "3",
"parentAttribute1": "parent3-1",
"parentAttribute2": "parent3-2",
"parentAttribute3": "parent3-3"
}
]
Expected payload after translation - Scenario 2 - Some attributes only
[
{
"id": "1",
"parentAttribute1": "parent1-1",
"childAttribute1": "inner1-1-1"
},
{
"id": "1",
"parentAttribute1": "parent1-1",
"childAttribute1": "inner1-2-1"
},
{
"id": "1",
"parentAttribute1": "parent1-1",
"childAttribute1": "inner1-3-1"
},
{
"id": "2",
"parentAttribute1": "parent2-1",
"childAttribute1": "inner2-1-1"
},
{
"id": "3",
"parentAttribute1": "parent3-1",
}
]
Tried to use reduce, group functions but was unable to merge them.
n/a
n/a
Upvotes: 1
Views: 762
Reputation: 5059
The key is to use the flatten plus a nested map. This way you can access both levels so you can operate with them.
%dw 2.0
output application/json
---
flatten(payload map
((parent, index) ->
if (parent.child?)
parent.child map ((child, index) -> (parent - "child") ++ child)
else
[parent]
)
)
For dw 1 this is the solution
%dw 1.0
%output application/json
---
flatten (payload map
((parent, index) ->
parent.child map ((child, index) -> (parent - "child") ++ child) when (parent.child?)
otherwise
[parent]
)
)
Upvotes: 2