jq - select objects with given key name

前端 未结 3 652
渐次进展
渐次进展 2021-02-01 22:38

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 o

3条回答
  •  被撕碎了的回忆
    2021-02-01 23:17

    The accepted answer also produces null for every object that doesn't have the key.

    What worked for me was:

    jq '..|objects|.updateDate//empty'
    

    The .updateDate//empty part means: if .updateDate is null (or false), skip it entirely.

    This of course wouldn't work if you expect your key to have values of false or null. In that case, use this:

    jq '..|objects|select(has("updateDate"))|.updateDate'
    

提交回复
热议问题