【问题标题】:Find most expensive book using JSONPath使用 JSONPath 查找最昂贵的书
【发布时间】:2022-01-22 06:27:52
【问题描述】:

鉴于 Goessner 示例 JSON:

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

我可以使用以下查询获得一本书的最高价格: $.max($.store.book[*].price)).

回复:22.99

如果我查询这个价格:$.store.book[?(@.price ==22.99)]

回复:

[
   {
      "category" : "fiction",
      "author" : "J. R. R. Tolkien",
      "title" : "The Lord of the Rings",
      "isbn" : "0-395-19395-8",
      "price" : 22.99
   }
]

但尝试将 max 表达式内联到查询中只会出现语法错误:

$.store.book[?(@.price ==$.max($.store.book[*].price)))]

这可以在单个 JSONPath 查询中实现吗?

【问题讨论】:

    标签: jsonpath


    【解决方案1】:

    Jayway JSONPath中,函数可以在路径的尾部调用

    $.store.book[?(@.price == $..book[*].price.max())]
    

    在线工具:https://jsonpath.herokuapp.com/

    注意:可能不适用于其他实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-23
      • 2010-09-20
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多