当在复杂的JSON数组和散列中搜索项时,比如:

[
    { "id": 1, "name": "One", "objects": [
        { "id": 1, "name": "Response 1", "objects": [
            // etc.
        }]
    }
]

是否有某种查询语言,我可以用来在[0]中找到一个项目。id = 3的对象?


当前回答

Json指针似乎也得到了越来越多的支持。

其他回答

JMESPath现在似乎非常流行(截至2020年),并解决了JSONPath的许多问题。它适用于许多语言。

我知道OP用javascript标记了这个问题,但在我的情况下,我看起来完全一样,但从Java后端(与Camel)。

有趣的是,如果您使用的是像Camel这样的集成框架,jsonPath也由特定的Camel组件支持,从Camel 2.13开始。

例子来自上面的Camel文档:

from("queue:books.new")
  .choice()
    .when().jsonpath("$.store.book[?(@.price < 10)]")
      .to("jms:queue:book.cheap")
    .when().jsonpath("$.store.book[?(@.price < 30)]")
      .to("jms:queue:book.average")
    .otherwise()
      .to("jms:queue:book.expensive")

使用起来很简单。

我认为JSONQuery是JSONPath的超集,因此在dojo中取代了JSONPath。还有RQL。

来自Dojo文档:

JSONQuery是JSONPath的扩展版本,具有其他特性 为安全性、易用性和全面的数据查询集 工具包括过滤,递归搜索,排序,映射,范围 选择,以及具有通配符字符串比较的灵活表达式 以及各种运算符。

JSONselect对这个问题有另一种观点(类似CSS选择器,而不是XPath),并有一个JavaScript实现。

ObjectPath是一种类似于XPath或JSONPath的查询语言,但由于嵌入了算术计算、比较机制和内置函数,它的功能要强大得多。 请看语法:

商店里所有的鞋子都是红色的,价格在50美元以下

美元. .鞋子。*[颜色为“红色”,价格< 50]

我知道的其他选择是

JSONiq specification, which specifies two subtypes of languages: one that hides XML details and provides JS-like syntax, and one that enriches XQuery syntax with JSON constructors and such. Zorba implements JSONiq. Corona, which builds on top of MarkLogic provides a REST interface for storing, managing, and searching XML, JSON, Text and Binary content. MarkLogic 6 and later provide a similar REST interface as Corona out of the box. MarkLogic 8 and later support JSON natively in both their XQuery and Server-side JavaScript environment. You can apply XPath on it.

HTH.