Consider the following json:
{
a: {
b: {
c: 1,
d: 2
}
}
}
How can I move all the properties of b to be under the parent a:
{
a: {
c: 1,
d: 2,
b: {}
}
}
Consider the following json:
{
a: {
b: {
c: 1,
d: 2
}
}
}
How can I move all the properties of b to be under the parent a:
{
a: {
c: 1,
d: 2,
b: {}
}
}
For this particular case, you could do this:
Here we're updating object
a
with the original contents withb
replaced with an empty object and combining it with the contents of the originalb
.If that was too hard to reason about, this might be easier to follow:
This is just a variant of @Jeff-Mercado's first solution, but may be slightly easier to follow (notably because there is only one reference to .b, and because the grouping on the RHS is explicit):
To do this recursively for any field use: