0

In the below JSON file I want to extract the empDetails.name where one or both iscomponentInSal is true .

JSON

[
*// Retrieve below `empDetails.name` as for one of the components we have `"iscomponentInSal": true`* 
  {
    "empDetails": {
    "name":"John",
      "compensation": {
        "salary": [
          {
            "component": "Basic",
            "iscomponentInSal": true
          },
          {
            "component": "HRA",
            "iscomponentInSal": false
          }
        ]
      }
    }
  },
*// Do not retrieve below `empDetails.name` as for both components we have `"iscomponentInSal": false`* 
{
    "empDetails": {
    "name":"Steve",
      "compensation": {
        "salary": [
          {
            "component": "Basic",
            "iscomponentInSal": false
          },
          {
            "component": "HRA",
            "iscomponentInSal": false
          }
        ]
      }
    }
  }
]    

I tried few ways to extract the results as below , but was not successfull

$.[salary[?(@.iscomponentInSal == true)])]

//This retrieves below json

[
  {
    "component": "Basic",
    "iscomponentInSal": true
  }
]
Akshay G
  • 2,070
  • 1
  • 15
  • 33
Mandy
  • 433
  • 2
  • 8
  • 25
  • _I tried few ways to extract the results but was not successfull_ You should include what you tried – Akshay G Feb 28 '23 at 10:47
  • Just curious to know how you determine if the salary is paid or not when you have both values for `isSalaryPaid` in the array. – Akshay G Feb 28 '23 at 10:51
  • @AkshayG updated the nodes for your understanding . Problem statement is same – Mandy Feb 28 '23 at 11:08

1 Answers1

1

There are different possible solutions using Jayway-JSONPath Filter Operators

using nested expressions and empty filter operator.

$[*].empDetails[?(@.compensation.salary[?(@.iscomponentInSal == true)] empty false)].name

using in filter operator; right side expression returns an array

$[*].empDetails[?(true in @.compensation.salary[*].iscomponentInSal)].name
Akshay G
  • 2,070
  • 1
  • 15
  • 33