简体   繁体   English

jq:从嵌套对象中选择键子集

[英]jq: selecting subset of keys from nested object

Input: 输入:

{"success": true, "results": {"a": …, "b": …, "c": …}}

Desired output, given I want to keep b : 给定我想要保留b期望输出:

{"success": true, "results": {"b": …}}

Things I tried: 我尝试过的事情:

$ jq 'del(select(.results.b | not))'
{"success": true, "results": {"a": …, "b": …, "c": …}}
# removes nothing from "results"

$ jq 'with_entries(select(.key == "success" or .key == "results.b"))'
{"success": true}
# nested comparison not understood; returns only "success"

Thanks! 谢谢!

Here is one solution: 这是一种解决方案:

.results |= {b}

Sample Run 样品运行

$ jq -M '.results |= {b}' <<< '{"success":true, "results":{"a": "…", "b": "…", "c": "…"}}'
{
  "success": true,
  "results": {
    "b": "…"
  }
}

Try it online at jqplay.org 在jqplay.org上在线尝试

Another way using and : 另一种使用

Code : 代码:

$ node<<EOF
var obj = $(</tmp/file.json);
delete obj.results.a;
delete obj.results.c;
console.log(JSON.stringify(obj));
EOF

OUTPUT : 输出:

{"success":true,"results":{"b":"bbb"}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM