【问题标题】:Using JSONPath get only 2 key-value pair when a condition is met满足条件时使用 JSONPath 仅获取 2 个键值对
【发布时间】:2020-11-04 10:02:51
【问题描述】:

使用以下 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
    }
  }
}

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

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

我从 $.store.book[?(@.price

【问题讨论】:

    标签: filter expression key-value jsonpath


    【解决方案1】:

    你很接近;这个表达式:

    $.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"
    ]
    

    【讨论】:

    • 谢谢杰克。请问您在哪里/如何测试它。这个:$.store.book[?(@.price link,这里是:link
    • 当然; you can test it here。第一个适用于 Jayway 和 Gatling 实现,第二个适用于 Goessner。
    猜你喜欢
    • 2020-03-31
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多