I have this Flattened Object
{
"abc.def.ghi": "foo",
"abc.def.jkl": "bar"
}
I want to write a dataweave to convert it into the original object, i.e.
{
"abc": {
"def": {
"ghi": "foo",
"jkl": "bar"
}
}
}
I am trying to avoid hard coding the keys because it is a quite large object, So I do not want something like this:
%dw 2.0
var test = {
"abc.def.ghi": "foo",
"abc.def.jkl": "bar"
}
output application/json
---
{
abc: {
def: {
ghi: test."abc.def.ghi",
jkl: test."abc.def.jkl"
}
}
}
Can I use some combination of available dataweave functions for achieving this?
Here is what I have tried so far:
%dw 2.0
var test = {
"abc.def.ghi": "foo",
"abc.def.jkl": "bar"
}
output application/json
---
test mapObject ((value, key) ->
(key as String splitBy ".")[-1 to 0]
reduce ((item, acc = value) ->
(item): acc
/*
First item=ghi,acc=foo => acc = {ghi: "foo"}
next item=def, acc={ghi: "foo"} => acc={def:{ghi:"foo"}}
*/
)
)
But this would generate some kind of separate pair of the nested JSON. Here is the output of the above code:
{
"abc": {
"def": {
"ghi": "foo"
}
},
"abc": {
"def": {
"jkl": "bar"
}
}
}