无法编写jsonpath查询以返回已过滤数组的第一个对象

Bla*_*zes 21 jsonpath

使用ashphy json路径评估器和以下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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想写一个过滤类别的查询,例如"小说",然后只返回第一个元素.我似乎无法弄明白.

例如,$..book[?(@.category == "fiction")]将返回3本书的数组,但我只想要第一本书:

{ "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      }
Run Code Online (Sandbox Code Playgroud)

可以吗?显而易见的事情似乎是在最后添加"[0]",但它不起作用.

Din*_*den 1

我检查了分析站点(注意:这是来自测试http://ashphy.com/JSONPathOnlineEvaluator/):

在此输入图像描述

放置 [0] 索引查找确实有效。该站点仅接受 jsonPath(jsonObj, query string) 参数内部的内容,因此索引引用必须超出该评估范围,而该站点不会执行此操作。

jsonPath(foo, '$..book[?(@.category == "fiction")]')[0]
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但这不是我想要的 - jsonPath 返回一个数组,并且 [0] 访问数组的第一个元素。我需要 jsonPath 来过滤一个元素。我无法直接访问该数组。 (2认同)