杰克:如何将孩子成员转移给父母? [英] Jq: How to move the child members to parent?
本文介绍了杰克:如何将孩子成员转移给父母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下json:
{
a: {
b: {
c: 1,
d: 2
}
}
}
如何将b的所有属性移到父a下:
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:
$ jq '.a |= (.b = {}) + .b' input.json
在这里,我们要用原始内容更新对象a
,用b
替换为空对象,然后将其与原始b
的内容组合起来.
Here we're updating object a
with the original contents with b
replaced with an empty object and combining it with the contents of the original b
.
如果这很难解释,那么可能会更容易理解:
If that was too hard to reason about, this might be easier to follow:
$ jq '.a |=
with_entries(if .key == "b"
then (.value = {}), (.value | to_entries[])
else .
end)' input.json
这篇关于杰克:如何将孩子成员转移给父母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文