Jq: Does jq support recursive descendants queries like `xpath` (`//foo`)?

Created on 12 Feb 2016  路  6Comments  路  Source: stedolan/jq

i have long-long json and i want access deep wrapped key, but i don't know their's patents trace

support

Most helpful comment

Yeah! Eureka!
path(.. | select(.name=="__base")?)

thanks for https://jqplay.org

All 6 comments

Yes: ..?|.foo?

To get the paths that match: path(..?|.foo?)

The FAQ General Questions section now has an entry regarding XPath's // and jq's ...

Thank you
[done]

It occurs to me that it'd be nice if jq had something closer to XPath syntax for matching elements of arrays, something like .<.foo==5> being the same as .[]|select(.foo==5). I.e., .<EXP> -> .[]|select(EXP).

For those of you not familiar with XML: XML is like JSON, but instead of being built around hash tables ("objects", "dicts"), it's built around arrays (after all, documents are sequences of text and mark-up). XPath and XSLT are functional aliases for dealing with XML: XPath is for writing queries against XML, and XSLT is for transforming XML. jq is to JSON as XPath and XSLT are to XML. Now, one common thing to do in XPath is to select document nodes that match some query, like //div[@class = 'foo'], which means: "select all element of type 'div' with 'class' attribute 'foo''. Writing that in jq is not hard, but perhaps more verbose that it should be: ..?|arrays|.[]|select(.element=="div" and .attributes.class=="foo"), and could instead be somewhat less verbose: ..?|.<.element=="div" and .attributes.class=="foo">?.

hm. in some cases it works. but it working not clear for me
for example in this json (part of estree of some my es6 class)
https://gist.github.com/a-x-/9ac7fa9f76cd07465f7e
expression path(..?|select(.name == "__base")) not workin'

i also tried another variants

Yeah! Eureka!
path(.. | select(.name=="__base")?)

thanks for https://jqplay.org

Was this page helpful?
0 / 5 - 0 ratings