I'd like to suggest adding support for some standard query languages like JSONPath and JSON pointers (RFC 6901) via cmdline switches. e.g.:
jq --jsonpath "/store/book[1]/title" books.json
The syntax should rather be
jq 'jsonpath("/store/book[1]/title")' books.json
With jsonpath/1 being a function that translates path expressions to jq filters.
Here you go with JSON Pointer:
~
jq 'include "jsonpointer"; pointer("/store/book/1/title")' books.json
~
can this be merged with jq so no include "jsonpointer"; is necessary?
This is the jsonpath statement I'd like to see supported:
The json is an array of items, each one having an id. I'd like to use jsonpath to find the item with id=X. e.g. jq '.[?(@.id = X)]'
@seansund wrote:
The json is an array of items, each one having an id. I'd like to use jsonpath to find the item with id=X
For those who don't know, jq already handles this type of query, and in fact makes it easy to distinguish between its two main variants:
all such items:
.[] | select(.id==X)
first such item:
first(.[] | select(.id==X))
Most helpful comment
Here you go with JSON Pointer:
~jq 'include "jsonpointer"; pointer("/store/book/1/title")' books.json
~