Search code examples
jsonpathjson-path-expression

How to get json path for the given requirement?


I need to find json path for the given json structure

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

Here I need to get the amount from performanceSet where the currency is USD and will only return the amount where it got first occurrence of such value?

It should return

[
   "231"
]

Solution

  • I don't think that is possible using jsonpath.

    You can use following jsonpath.

    $.Account[0].investment[0].performance[0].performanceSet[?(@.currency=='USD')]
    

    This will give you

    [
      {
        "amount": "231",
        "currency": "USD"
      },
      {
        "amount": "233",
        "currency": "USD"
      }
    ]
    

    Here you can programmitically select the first element and further read the amount.