0

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"
]
Smile
  • 3,832
  • 3
  • 25
  • 39
Ninja
  • 433
  • 3
  • 10
  • 1
    I think that can not be possible with json-path.Let's see if there is something for that. –  Aug 13 '20 at 04:47

2 Answers2

1

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.

Smile
  • 3,832
  • 3
  • 25
  • 39
0

You can try converting the json to XML using js2xml and then use xpath on the same. XPath is much more powerful

dipanjanb
  • 51
  • 3