json路径返回与 predicate 匹配的整个json

oyjwcjzk  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(428)

我有一个用例,需要json路径来返回与其 predicate 匹配的整个json结构。

  1. {
  2. "store" : {
  3. "book" : [
  4. {
  5. "category" : "reference",
  6. "author" : "Nigel Rees",
  7. "title" : "Sayings of the Century",
  8. "price" : 8.95,
  9. },
  10. {
  11. "category" : "fiction",
  12. "author" : "Evelyn Waugh",
  13. "title" : "Sword of Honour",
  14. "price" : 12.99
  15. },
  16. {
  17. "category" : "fiction",
  18. "author" : "Herman Melville",
  19. "title" : "Moby Dick",
  20. "isbn" : "0-553-21311-3",
  21. "price" : 8.99
  22. },
  23. {
  24. "category" : "fiction",
  25. "author" : "J. R. R. Tolkien",
  26. "title" : "The Lord of the Rings",
  27. "isbn" : "0-395-19395-8",
  28. "price" : 22.99
  29. }
  30. ],
  31. "bicycle" : {
  32. "color" : "red",
  33. "price" : 19.95
  34. }
  35. },
  36. "expensive" : 10
  37. }

这是我的 json path expression ```
$.store.book[?(@.category in ['reference'])]

  1. 返回

[
{
"category" : "reference",
"author" : "Nigel Rees",
"title" : "Sayings of the Century",
"price" : 8.95
}

  1. ]
  2. 但我想要整条路,如下图所示,

{
"store" : {
"book" : [
{
"category" : "reference",
"author" : "Nigel Rees",
"title" : "Sayings of the Century",
"price" : 8.95
}

  1. ]

}
}

wlsrxk51

wlsrxk511#

这不是json路径通常可以做的事情,但请检查您的具体实现。
通常,json path可以返回匹配项或指向它们的路径。不支持按原始结构中的显示嵌套匹配。
然而,您可能能够从路径中推断出您需要什么。该项目的路径将是 $.store.book[0] . 要重建所需的对象,您需要。。。

  1. $ // start, no impact
  2. .store // create an object with "store"
  3. .book // create an object with "book"
  4. [0] // since this is the last thing, replace this with the match

从这一点来看,如果匹配进一步深入到数组中,而不是在开始时,那么尝试支持这一点似乎会有问题。例如,如果您已匹配 [2] 而不是 [0] ,您仍然会得到与您发布的相同的嵌套结构(不是在这里讨论这是如何工作的;只是解释为什么现在没有。)

相关问题