jq-选择具有给定键名的对象 [英] jq - select objects with given key name
本文介绍了jq-选择具有给定键名的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有多个级别的任意结构,等等.我需要选择所有包含名为updateDate
的键的对象.我如何用jq做到这一点?我想出了一种方法,但是当它访问必须被grep删除的叶子时,也会在无效数据类型上产生错误:
I've got an arbitrary structure with many levels, etc. I need to select all objects that contain a key named updateDate
. How do I do that with jq? I came up with one way but it also produces errors on invalid data types when it visits a leaf which I have to grep out:
jq 'recurse(.[]) | has("updateDate")' | grep -Fv error
我不太了解如何检查类型或叶子,我怀疑有更简单的方法来实现我想要的吗?
I don't really understand how to also check for types or leaves and I suspect there is a simpler way to achieve what I want?
推荐答案
在1.4中,您可以:
jq '..|.updateDate?'
如果您坚持使用1.3,则可以使用更长的程序,如下所示:
If you're stuck with 1.3 you can use a longer program like so:
jq 'recurse(if type == "array" or type = "object" then .[] else empty end) | if type == "object" then .updateDate else empty end'
这篇关于jq-选择具有给定键名的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文