0

A call to 3rd party API returns a JSON like below:

{
  "id": 515,
  "name": "SamplePlan",
  "is_completed": false,
  "project_id": 9,
  "url": "https://domain.ad.com/test/index.php?/plans/view/515",
  "entries": [
    {
      "id": "7857aab1-b4a3-460f-86ae-0392a128c6f5",
      "suite_id": 108,
      "name": "ANDROID 6 BRANCH 21:10",
      "runs": [
        {
          "id": 1184,
          "suite_id": 108,
          "name": "ANDROID 6 BRANCH 21:10",
          "entry_id": "7857aab1-b4a3-460f-86ae-0392a128c6f5",
        }
      ]
    },
    {
      "id": "c68a3358-dacd-4b5a-b1a8-3632b4df6476",
      "suite_id": 108,
      "name": "ANDROID 7 BRANCH 23:10",
      "runs": [
        {
          "id": 1393,
          "suite_id": 108,
          "name": "ANDROID 7 BRANCH 23:10",
          "entry_id": "c68a3358-dacd-4b5a-b1a8-3632b4df6476",
        }
      ]
    },
    {
      "id": "bb7318fe-f04f-42e9-985b-19aae697eba6",
      "suite_id": 108,
      "name": "ANDROID 8 BRANCH 13:10",
      "runs": [
        {
          "id": 1506,
          "suite_id": 108,
          "name": "ANDROID 8 BRANCH 13:10",
          "entry_id": "bb7318fe-f04f-42e9-985b-19aae697eba6",
        }
      ]
    }
  ]
}

This shows that for the test plan SamplePlan with id 515, it has 3 entries with ids 7857aab1-b4a3-460f-86ae-0392a128c6f5, c68a3358-dacd-4b5a-b1a8-3632b4df6476, and bb7318fe-f04f-42e9-985b-19aae697eba6.

Out of this complete response, I want to fetch one specific entry id from all the entries. I can think of choosing one unique name/value pair for applying such condition.

For example, if I want to pull the first entry id i.e. 7857aab1-b4a3-460f-86ae-0392a128c6f5, then I think I will be doing like:

if (inside "entries" and if name is (ANDROID 6 BRANCH 21:10) then ) {
      pull the entry id of this case;
}

Another approach can be:

if (inside "entries" and further inside "runs" and if (id ==1184) then) {
      pull the entry id of this case which is again 7857aab1-b4a3-460f-86ae-0392a128c6f5
}

Is there a a way to implement such thing in Java? An example considering such scenario would be great.

Pratik Jaiswal
  • 309
  • 7
  • 26

1 Answers1

0

You could use a library like JsonPath. It allows you to perfom queries on your json objects similar to xpath on xml-based content.

Your first example would look somewhat like this (untested!):

$.entries[?(@.name == "ANDROID 6 BRANCH 21:10")].id

Edit: Look at this answer

Community
  • 1
  • 1
Abaddon666
  • 1,533
  • 15
  • 31