如何为给定 json 定义 json 路径?

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"
            }
          ]
          
        }
      ]
    }
   ]
 }
]
}

所以我必须select货币为美元的金额?

然后我尝试将其作为“$.Account..investment.performance..performanceSet.amount[?(@.currency=~/.*USD/)]”

这个 JsonPath 应该可以工作:

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

测试于:

{
   "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"
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

哪个returns:

[
  "231",
  "235"
]

这个网站是一个很好的尝试方法:https://jsonpath.com/

阅读文档:https://github.com/intuit/karate#jsonpath-filters

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

你可以这样试试

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