Search code examples
jsonjsonpath

Filter nested JSON object using JSONPath with Condition


I have a below json object and need to get only c_Internet child object with a condition where the PIID is having value "1234567".

{
  "OrderComponentInfo": {
    "members": {
      "ADD": {
        "DIA": [
          {
            "c_Internet": {
              "PIID": "1234567",
              "IPv6_Multiple_Path_Needed": ""
            },
            "Test": {
              "PIID": "1234567",
              "INT_Acc_MSP_ID": "",
              "ConnectedService": "INTERNET"
            }
          },
          {
             "c_Internet": {
              "PIID": "7658964",
              "IPv6_Multiple_Path_Needed": ""
            },
            "Test": {
              "PIID": "7658964",
              "INT_Acc_MSP_ID": "",
              "ConnectedService": "INTERNET"
            }
          }
        ]
      }
    }
  }
}

I trued below query but dint work

$..ADD.DIA[?(@.PIID = "1234567")][['c_Internet']]

Thanks in advance.


Solution

  • you have to use c_Internet.PIID not just PIID

    const id="1234567";
    
    $..ADD.DIA[?( @.c_Internet.PIID == id )]["c_Internet"]
    

    result

    [
      {
        "PIID": "1234567",
        "IPv6_Multiple_Path_Needed": ""
      }
    ]