首页 > 解决方案 > 带有正则表达式的 JSONPath 过滤器返回数组中的所有值而不是子集

问题描述

我试图弄清楚如何编写一个 JSONPath 过滤器,它将选择属性以子字符串结尾的数组的成员。我正在用这个工具试一试

该网站为您提供的示例数据是

{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

我希望这个过滤器只给我“iPhone”类型的电话号码。

$.phoneNumbers[?(@.type =~ /ne$/ )]

相反,它给了我两个电话号码。谁能告诉我为什么?

标签: regexjsonpath

解决方案


凯蒂-抱歉延迟回答。

$..phoneNumbers[?(@['type'] == 'iPhone')].number

希望能解决这个问题。

也许这个解决方案对其他人有用。


推荐阅读