Search code examples
javaselenium-gridjsonpathjson-extract

Extract the count of session with null values with condition of platform name


I want to extract the COUNT OF session which are of null values from the below json. I tried with online json path extractor its work but the tried on java code with JsonPath lib, its showing parameter is not defind.

*{
  "value": {
    "ready": true,
    "message": "Selenium Grid ready.",
    "nodes": [
      {
        "id": "eaef55e8-18a3-490f-a6a5-0a1cce762e80",
        "uri": "xyz",
        "maxSessions": 4,
        "osInfo": {
          "arch": "amd64",
          "name": "Windows 10",
          "version": "10.0"
        },
        "heartbeatPeriod": 60000,
        "availability": "UP",
        "version": "4.3.0 (revision a4995e2c09*)",
        "slots": [
          {
            "id": {
              "hostId": "eaef55e8-18a3-49f-a6a5-0acce762e80",
              "id": "9e4bce35-f596-476f-ab0f-b5df17e02099"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "Windows 10"
            }
          },
          {
            "id": {
              "hostId": "eaef55e8-18a3-490f-a6a5-0a5cce762e80",
              "id": "bab6efd1-ed0d-450a-85db-13cbd6c8f6e6"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "Windows 10"
            }
          },
          {
            "id": {
              "hostId": "eaef55e8-18a3-490f-a6a5-0acce762e80",
              "id": "5307491a-3ce7-4dc2-98b7-2e8dd1a9dbc2"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "Windows 10"
            }
          },
          {
            "id": {
              "hostId": "eaef55e8-18a3-490f-a6a5-05cce762e80",
              "id": "0856bb3b-3a47-44bf-a532-4b4f0298ef95"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "Windows 10"
            }
          }
        ]
      },
      {
        "id": "8311f460-496b-4181-9960-19bd5cfaa125",
        "uri": "http:\u002f\u002f172.18.32.1:5555",
        "maxSessions": 4,
        "osInfo": {
          "arch": "amd64",
          "name": "Windows 10",
          "version": "10.0"
        },
        "heartbeatPeriod": 60000,
        "availability": "UP",
        "version": "4.3.0 (revision a4995e2c09*)",
        "slots": [
          {
            "id": {
              "hostId": "8311f460-496b-4181-9960-19bdcfaa125",
              "id": "39b50d92-9cb8-47a1-985b-b3e965453a0d"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "Windows 10"
            }
          },
          {
            "id": {
              "hostId": "8311f460-496b-4181-990-19bdfaa125",
              "id": "76e691c7-fab5-493a-bc2f-317a2776e5cf"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "Windows 10"
            }
          },
          {
            "id": {
              "hostId": "8311f460-496b-4181-9960-19bd5cfaa125",
              "id": "0692a825-2b83-462e-9a5c-dfcae77c033a"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "Windows 10"
            }
          },
          {
            "id": {
              "hostId": "8311f460-496b-4181-9960-19bd5cfaa125",
              "id": "10aa0cd9-e9a5-4070-87b6-52a4201e9bc0"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "Windows 10"
            }
          }
        ]
      }
    ]
  }
}*

Could you please help me to get the exact path which will work on java too with the given condition.

Thank you in advance.


Solution

  • You may consider another library Josson for the solution.

    https://github.com/octomix/josson

    Deserialization

    Josson josson = Josson.fromJsonString(
        "{" +
        "  \"value\": {" +
        "    \"ready\": true," +
        "    \"message\": \"Selenium Grid ready.\"," +
        "    \"nodes\": [" +
        "      {" +
        "        \"id\": \"eaef55e8-18a3-490f-a6a5-0a1cce762e80\"," +
        "        \"uri\": \"xyz\"," +
        "        \"maxSessions\": 4," +
        "        \"osInfo\": {" +
        "          \"arch\": \"amd64\"," +
        "          \"name\": \"Windows 10\"," +
        "          \"version\": \"10.0\"" +
        "        }," +
        "        \"heartbeatPeriod\": 60000," +
        "        \"availability\": \"UP\"," +
        "        \"version\": \"4.3.0 (revision a4995e2c09*)\"," +
        "        \"slots\": [" +
        "          {" +
        "            \"id\": {" +
        "              \"hostId\": \"eaef55e8-18a3-49f-a6a5-0acce762e80\"," +
        "              \"id\": \"9e4bce35-f596-476f-ab0f-b5df17e02099\"" +
        "            }," +
        "            \"lastStarted\": \"1970-01-01T00:00:00Z\"," +
        "            \"session\": null," +
        "            \"stereotype\": {" +
        "              \"browserName\": \"chrome\"," +
        "              \"platformName\": \"Windows 10\"" +
        "            }" +
        "          }," +
        "          {" +
        "            \"id\": {" +
        "              \"hostId\": \"eaef55e8-18a3-490f-a6a5-0a5cce762e80\"," +
        "              \"id\": \"bab6efd1-ed0d-450a-85db-13cbd6c8f6e6\"" +
        "            }," +
        "            \"lastStarted\": \"1970-01-01T00:00:00Z\"," +
        "            \"session\": null," +
        "            \"stereotype\": {" +
        "              \"browserName\": \"chrome\"," +
        "              \"platformName\": \"Windows 10\"" +
        "            }" +
        "          }," +
        "          {" +
        "            \"id\": {" +
        "              \"hostId\": \"eaef55e8-18a3-490f-a6a5-0acce762e80\"," +
        "              \"id\": \"5307491a-3ce7-4dc2-98b7-2e8dd1a9dbc2\"" +
        "            }," +
        "            \"lastStarted\": \"1970-01-01T00:00:00Z\"," +
        "            \"session\": null," +
        "            \"stereotype\": {" +
        "              \"browserName\": \"chrome\"," +
        "              \"platformName\": \"Windows 10\"" +
        "            }" +
        "          }," +
        "          {" +
        "            \"id\": {" +
        "              \"hostId\": \"eaef55e8-18a3-490f-a6a5-05cce762e80\"," +
        "              \"id\": \"0856bb3b-3a47-44bf-a532-4b4f0298ef95\"" +
        "            }," +
        "            \"lastStarted\": \"1970-01-01T00:00:00Z\"," +
        "            \"session\": null," +
        "            \"stereotype\": {" +
        "              \"browserName\": \"chrome\"," +
        "              \"platformName\": \"Windows 10\"" +
        "            }" +
        "          }" +
        "        ]" +
        "      }," +
        "      {" +
        "        \"id\": \"8311f460-496b-4181-9960-19bd5cfaa125\"," +
        "        \"uri\": \"http:\\u002f\\u002f172.18.32.1:5555\"," +
        "        \"maxSessions\": 4," +
        "        \"osInfo\": {" +
        "          \"arch\": \"amd64\"," +
        "          \"name\": \"Windows 10\"," +
        "          \"version\": \"10.0\"" +
        "        }," +
        "        \"heartbeatPeriod\": 60000," +
        "        \"availability\": \"UP\"," +
        "        \"version\": \"4.3.0 (revision a4995e2c09*)\"," +
        "        \"slots\": [" +
        "          {" +
        "            \"id\": {" +
        "              \"hostId\": \"8311f460-496b-4181-9960-19bdcfaa125\"," +
        "              \"id\": \"39b50d92-9cb8-47a1-985b-b3e965453a0d\"" +
        "            }," +
        "            \"lastStarted\": \"1970-01-01T00:00:00Z\"," +
        "            \"session\": null," +
        "            \"stereotype\": {" +
        "              \"browserName\": \"chrome\"," +
        "              \"platformName\": \"Windows 10\"" +
        "            }" +
        "          }," +
        "          {" +
        "            \"id\": {" +
        "              \"hostId\": \"8311f460-496b-4181-990-19bdfaa125\"," +
        "              \"id\": \"76e691c7-fab5-493a-bc2f-317a2776e5cf\"" +
        "            }," +
        "            \"lastStarted\": \"1970-01-01T00:00:00Z\"," +
        "            \"session\": null," +
        "            \"stereotype\": {" +
        "              \"browserName\": \"chrome\"," +
        "              \"platformName\": \"Windows 10\"" +
        "            }" +
        "          }," +
        "          {" +
        "            \"id\": {" +
        "              \"hostId\": \"8311f460-496b-4181-9960-19bd5cfaa125\"," +
        "              \"id\": \"0692a825-2b83-462e-9a5c-dfcae77c033a\"" +
        "            }," +
        "            \"lastStarted\": \"1970-01-01T00:00:00Z\"," +
        "            \"session\": null," +
        "            \"stereotype\": {" +
        "              \"browserName\": \"chrome\"," +
        "              \"platformName\": \"Windows 10\"" +
        "            }" +
        "          }," +
        "          {" +
        "            \"id\": {" +
        "              \"hostId\": \"8311f460-496b-4181-9960-19bd5cfaa125\"," +
        "              \"id\": \"10aa0cd9-e9a5-4070-87b6-52a4201e9bc0\"" +
        "            }," +
        "            \"lastStarted\": \"1970-01-01T00:00:00Z\"," +
        "            \"session\": null," +
        "            \"stereotype\": {" +
        "              \"browserName\": \"chrome\"," +
        "              \"platformName\": \"Windows 10\"" +
        "            }" +
        "          }" +
        "        ]" +
        "      }" +
        "    ]" +
        "  }" +
        "}");
    

    Query

    // Get all the slots with session=null
    JsonNode node = josson.getNode(
        "value.nodes.slots[session=null & stereotype.platformName='Windows 10']*");
    System.out.println(node.toPrettyString());
    
    // Get the COUNT
    node = josson.getNode(
        "value.nodes.slots[session=null & stereotype.platformName='Windows 10']*.size()");
    System.out.println(node.toPrettyString());
    
    // Use wildcard search
    node = josson.getNode(
        "*().slots[session=null & stereotype.platformName='Windows 10']*.size()");
    System.out.println(node.toPrettyString());