How To Use Wildcard In JSON Path
asimjalis opened this issue · 6 comments
How can I use wildcards in queries?
For example consider this.
echo '{"users":[{"name":"s1","id":1},{"name":"s2","id":2}]}'|jid
I can query users[0].id
or users[1].id
.
How can I get all the ids? I tried users[*].id
which didn’t work.
In Clojure I use specter for this which is able to handle wildcards using ALL
.
Here is a use case for this. Suppose I get the JSON from AWS using awscli
containing all the EC2 instances. I want to extract the public IP addresses of all the instances.
That's nice..
@asimjalis That's really a great idea.
jid haven't implement wildcards query (like []
(in jq) or [*]
.) yet.
But I want to implement it in the next version ( or other PR).
I was wondering why not just use jq
go-lang bindings list of bindings?
Seems jid -> json_manager
uses simplejson under the hood (https://github.com/bitly/go-simplejson) to parse out keywords in the json struct and build a list of suggestions for auto completion. With simplejson approach, it feels like a full re-implementation of existing golang JSONPath implementations 1, 2 is required?
Perhaps simplejson
can be replaced in the json_manager
and suggestion
implementations to use a more powerful library?
just noticed - from the hn comments - https://github.com/fiatjaf/jiq
However, at this stage jiq
does not provide suggestions and auto-completion.
@fiatjaf - do you intend to support suggestions? If so, jiq
needs to get a list of keys from json (jid
does this using simplejson.Map
and uses regular expressions to determine auto completion keywords).
Also, ideally jiq
delays running jq
until there are no syntax errors
I intend to support suggestions. The code from @simeji is all there to support it, so it will probably be easy, I just need to understand it a little better. I would gladly accept PRs in this direction.
About query validation I'm in a dead end, I don't know how to do them. jq
is a programming language, and there's no good way to validate a program without running it. Unless we perform validation by running the query over a dummy input and ignoring errors arising from the dependency on certain structures not present on the dummy input (like what I do here), but it doesn't seem to be an improvement. Please, let me know if there is a better way.
UPDATE: Autocomplete and suggestions are now live on https://github.com/fiatjaf/jiq.