如何删除除jq以外的所有键? [英] How do I remove all keys except one with jq?

查看:97
本文介绍了如何删除除jq以外的所有键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个对象列表,其中包含许多我不想要的键:

Given a list of objects, with many keys I don't want:

[{
    "name": "Alice",
    "group": "Admins",
    "created": "2014"
}, {
    "name": "Bob",
    "group": "Users",
    "created": "2014"
}]

如何过滤这些对象以仅包含我想要的键?

How do I filter these objects to only include keys I want?

[{
    "name": "Alice"
}, {
    "name": "Bob"
}]

我已经尝试过jq '.[].name',但这会提取值,而不是保留对象.

I've tried jq '.[].name' but that extracts the values, rather than preserving the objects.

推荐答案

您可以使用map()函数来过滤任何键:

You can use the map() function to filter any key:

jq 'map({name: .name})'

更新

@WilfredHughes 建议:上面的过滤器可以缩写为:

Update

Suggested by @WilfredHughes: The above filter can be abbreviated as follows:

jq 'map({name})'

这篇关于如何删除除jq以外的所有键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆