如何获取对象的前 x 深度并使用 jq 删除其余深度?
我有以下示例:
{
"Service1": {
"Production": {
"Location 1": {
"b2d1": {
"clusters": {
"Datacenter2": []
},
"prod": {
"clusters": {
"Datacenter1": []
}
}
}
},
"Service2": {
"Production": {
"Location 1": {
"dr1": {
"clusters": {
"Datacenter3": []
},
"prod": {
"clusters": {
"Datacenter1": []
}
}
}
}
}
}
}
}
}
就我而言,我想要获得前 3 个级别(想要删除所有条目中以下位置的所有内容。
从问题和示例来看,并不太清楚应该如何处理数组,因此以下内容可能会令人感兴趣,因为它清楚地区分了 JSON 对象和所有其他对象:
# If the input is an object then:
# if depth <= 0 then emit {}, otherwise map_values(retain(depth - 1));
# otherwise echo the input.
def retain(depth):
if type == "object"
then if depth <= 0 then {}
else map_values(retain(depth - 1))
end
else .
end;
retain(3)
对于示例输入,输出将是:
{
"Service1": {
"Production": {
"Location 1": {},
"Service2": {}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)