如何在每个数组中连接 jq 路径

how to concat jq paths in each array

这是以 'headline' 结尾的 json 路径 我有:

jq -c 'paths | select(.[-1] == "headline")' news.json

["data","legacyCollection","collectionsPage","stream","edges",0,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",1,"node","headline"]
["data","legacyCollection","collectionsPage","stream","edges",2,"node","headline"]

我怎样才能更进一步,即在一条路径中连接所有元素。 将我的输出从:

["data","legacyCollection","collectionsPage","stream","edges",2,"node","headline"]

'.data.legacyCollection.collectionsPage.stream.edges[2].node'

删除最后一个元素,在 strings 前加一个点,将 numbers 括在方括号中,join 数组:

.[:-1] | map(strings = ".\(.)" | numbers = "[\(.)]") | join("")
".data.legacyCollection.collectionsPage.stream.edges[0].node"
".data.legacyCollection.collectionsPage.stream.edges[1].node"
".data.legacyCollection.collectionsPage.stream.edges[2].node"

Demo