json - 如何在 jq 中解构后重建单个对象

标签 json object jq

我收到一个相当大的 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/

相关文章:

java - Java 中从字符串到字符串数组的 JSON 映射

json - 如何使用 jq 按内部字典的值过滤列表?

json - JQ 中的括号用于 .key

json - jq --arg 变量在 select() 中的引用字符串中使用

java - Spring REST @RequestBody 总是空的

jquery - Facebook 客户端永久访问 token

PHP/json_encode : dealing with mixed arrays and objects with numeric properties

javascript - 为什么我的 JS 循环会覆盖对象中以前的条目?

python - Python方法调用问题

object - OpenCV - 在 OpenCV 中训练 LatentSVMDetector 模型(Pedro Felzenszwalb 的算法)