使用 JSONPath 仅在满足条件时获得 2 key-value 对

Using JSONPath get only 2 key-value pair when a condition is met

与下面json object.

{ "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
    }
  }
}

使用 JSONPath。对于所有价格低于 13.00 的书籍,是否有任何表达式只能获得 key-value 对“作者”和“标题”。结果将类似于:

[{
    "author": "Nigel Rees",
    "title": "Sayings of the Century"
  }, {
    "author": "Evelyn Waugh",
    "title": "Sword of Honour"
  }, {
    "author": "Herman Melville",
    "title": "Moby Dick"
  }
]

我从 $.store.book[?(@.price < 13)] 开始,但这得到了每本书成本低于 13.00 的所有元素。不是期望的行为。非常感谢任何帮助。

你很亲密;这个表达式:

$.store.book[?(@.price < 13)]["author","title"]

或者,取决于实施

$.store.book[?(@.price < 13)][]author,title

应该得到你

[
   {
      "author" : "Nigel Rees",
      "title" : "Sayings of the Century"
   },
   {
      "author" : "Evelyn Waugh",
      "title" : "Sword of Honour"
   },
   {
      "author" : "Herman Melville",
      "title" : "Moby Dick"
   }
]

[
   "Nigel Rees",
   "Sayings of the Century",
   "Evelyn Waugh",
   "Sword of Honour",
   "Herman Melville",
   "Moby Dick"
]