且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JSON和JQ中获取具有特定键的所有值的数组

更新时间:2023-02-23 09:56:33

您可以使用getpath函数.使用paths标识通向.comments的所有路径并获取路径的值

You can use the getpath function. Use paths to identify all the paths leading upto .comments and get the paths' value

jq '[ getpath ( paths | select( .[-1] == "comments" ) ) ]'

或者使用递归下降来过滤包含.comments的对象并获取其值

Or use a recursive descent to filter objects containing .comments and get its value

jq '[ recurse | select(has("comments")?).comments ]'