Here is the json paths which ends with 'headline' I have got:
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"]
How can I get one step further which is concat all elements in a single path. to turn my output from:
["data","legacyCollection","collectionsPage","stream","edges",2,"node","headline"]
to
'.data.legacyCollection.collectionsPage.stream.edges[2].node'
CodePudding user response:
Remove the last element, prepend a dot to strings
, wrap numbers
in brackets, and join
the array:
.[:-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"