Ninja
Ninja

Reputation: 453

How can I define jsonPath for given json?

 {
"name": "ninja",
"contry": "India",
"Account": [
{
  "id": "123",
  "orgId": 223,
  "investment": [
    {
      "invetmentId": "111",
      "name": "India tech",
      "performance": [
        {
          "id": "123",
          "performanceSet": [
            {
              "amount": "231",
              "currency": "USD"
            },
            {
              "amount": "250",
              "currency": "IND"
            }
          ]
          
        }
      ]
    }
   ]
 }
]
}

So I have to select the amount where the currency is USD?

And I tried it as "$.Account..investment.performance..performanceSet.amount[?(@.currency=~/.*USD/)]"

Upvotes: 0

Views: 128

Answers (3)

user14079473
user14079473

Reputation:

You can try it this way

$.Account..investment.performance..performanceSet.amount[?(@.currency=~/.*USD/)]

Upvotes: 0

pavelsaman
pavelsaman

Reputation: 8322

This JsonPath should work:

$..performanceSet[?(@.currency == "USD")].amount

Tested on:

{
   "name":"ninja",
   "contry":"India",
   "Account":[
      {
         "id":"123",
         "orgId":223,
         "investment":[
            {
               "invetmentId":"111",
               "name":"India tech",
               "performance":[
                  {
                     "id":"123",
                     "performanceSet":[
                        {
                           "amount":"231",
                           "currency":"USD"
                        },
                        {
                           "amount":"250",
                           "currency":"IND"
                        }
                     ]
                  }
               ]
            },
            {
               "invetmentId":"112",
               "name":"India tech 2",
               "performance":[
                  {
                     "id":"124",
                     "performanceSet":[
                        {
                           "amount":"235",
                           "currency":"USD"
                        },
                        {
                           "amount":"250",
                           "currency":"IND"
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

which returns:

[
  "231",
  "235"
]

A good way to try it out is this site: https://jsonpath.com/

Upvotes: 3

Peter Thomas
Peter Thomas

Reputation: 58058

Read the docs: https://github.com/intuit/karate#jsonpath-filters

* def temp = $..performanceSet[?(@.currency=='USD')]
* match temp[0].amount == '231'

Upvotes: 2

Related Questions