1

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.

SacTan
  • 379
  • 2
  • 5
  • 18
  • Can you include the jsonpath query that works online but not in java lib? – Akshay G Jun 29 '22 at 11:44
  • This work online, $..slots[?(@.session= 'null' && @.stereotype.platformName=='Windows 10')] – SacTan Jun 29 '22 at 12:12
  • with reserassured JSON getting the below error, Exception in thread "main" java.lang.IllegalArgumentException: Invalid JSON expression: Script1.groovy: 1: Unexpected input: '[' @ line 1, column 35. $..slots[?(@.session= 'null' && @.stereotype.platformName=='Windows 10') – SacTan Jun 29 '22 at 12:16
  • try `$..slots[?(@.session == null && @.stereotype.platformName == 'Windows 10')]` – Akshay G Jul 02 '22 at 07:55
  • Use https://jsonpath.herokuapp.com/ Online Test Tool for Java library Jayway JSONPath – Akshay G Jul 02 '22 at 07:57

2 Answers2

0

If you are using Restassured's JsonPath, i.e. "io.restassured.path.json.JsonPath", please remember that it uses a Gpath expression and hence the expression for JayWay's JsonPath would not work here.

For your json payload, the below code should work using RestAssured:

import java.util.List;
import java.util.Map;
import io.restassured.path.json.JsonPath;
...
public static int countOfSessions(String json) {
        JsonPath js = new JsonPath(json);
        List<Map> ls = js.param("sessVal", null).get("value.nodes.flatten().slots.flatten().findAll {s -> s.session == sessVal}.session");
        return ls.size();
        
    }
Bhanu
  • 53
  • 1
  • 6
0

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());
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8