我收到一个相当大的 json 文件,其中包含许多仅出于历史原因而不再使用的属性。为了简化这个文件,我使用了 jq
,用 to_entries
解构了 json,但现在我不知道如何用剩余的子对象重建我的对象。
例子如下:
输入
{
"empty1": [],
"empty2": [],
"full1": "test",
"full2": { "a": 1, "b": 2 }
}
当前过滤器:
to_entries[] | select((.value | length) > 0) | { (.key) : .value }
电流输出
{"full1":"test"}
{"full2":{"a":1,"b":2}}
期望的输出
{
"full1": "test",
"full2": {
"a": 1,
"b": 2
}
}
最佳答案
使用 with_entries()
更短,你可以用它来做
jq 'with_entries(select((.value | length) > 0))' json
对于您的问题,from_entries
执行与to_entries
相反的转换。使用 with_entries(foo)
是 to_entries | 的简写 map (富) | from_entries
语法。
关于json - 如何在 jq 中解构后重建单个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53739817/