1

Below is the JSON file I am trying to read nodes from. I am trying to perform contains operation. I do not want to use equals option in my json path like this - $.[?(@.name=="College Graduate - Ford")]

Could you please help me in meeting my requirement?

[
   {
      "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
   }
]
Sree
  • 1,694
  • 1
  • 18
  • 29
Srinivasan Ramu
  • 1,033
  • 4
  • 11
  • 23

2 Answers2

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

Here I am using the test the RegExp test( ) method (which JSONPath evals behind the scenes) . This will perform the contains operation.

enter image description here

enter image description here

Sree
  • 1,694
  • 1
  • 18
  • 29
0

you can use filter to get array from array which elements satisfy the condition.

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

result:

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