如何获取包含特定值的JSON节点?

时间:2018-05-13 03:43:21

标签: json jsonpath

下面是我试图从中读取节点的JSON文件。我正在尝试执行包含操作。我不想在我的json路径中使用equals选项 - $。[?(@。name ==“College Graduate - Ford”)]

你能帮我满足我的要求吗?

[
   {
      "incentiveId" : 123,
      "autoApplied" : "false",
      "name" : "College Graduate - Ford",
      "amount" : 750,
      "expirationDate" : "2018-12-31",
      "minimumTerm" : null,
      "maximumTerm" : null,
      "groupAffiliation" : "College/Student",
      "previousOwnership" : null
   },
   {
      "incentiveId" : 456,
      "autoApplied" : "false",
      "name" : "Lease Loyalty - Ford",
      "amount" : 500,
      "expirationDate" : "2018-07-09",
      "groupAffiliation" : null,
      "previousOwnership" : "Lease Loyalty"
   },
   {
      "incentiveId" : 789,
      "autoApplied" : "false",
      "name" : "Customer Cash - Ford",
      "amount" : 1000,
      "expirationDate" : "2018-06-04",
      "groupAffiliation" : null,
      "previousOwnership" : null
   },
   {
      "incentiveId" : 222,
      "autoApplied" : "false",
      "name" : "Military - Ford",
      "amount" : 1000,
      "expirationDate" : "2018-12-31",
      "groupAffiliation" : "Military",
      "previousOwnership" : null
   }
]

2 个答案:

答案 0 :(得分:2)

$.[?(/College/.test(@.name))]

这里我使用了RegExp test()方法的测试(JSONPath在幕后进行了验证)。这将执行包含操作。

enter image description here

enter image description here

答案 1 :(得分:0)

你可以使用filter从数组中获取哪些元素满足条件。

colleagues = json.filter(node => node.name.match("College"))

结果:

[
  {
    "incentiveId": 123,
    "autoApplied": "false",
    "name": "College Graduate - Ford",
    "amount": 750,
    "expirationDate": "2018-12-31",
    "minimumTerm": null,
    "maximumTerm": null,
    "groupAffiliation": "College/Student",
    "previousOwnership": null
  }
]