Nested iteration over JSON using groovy gpath

543 views
Skip to first unread message

Riyas Valiya

unread,
Sep 1, 2018, 11:11:35 AM9/1/18
to REST assured
I've posted the same question here on StackOverflow. If someone has an answer, please post it there too so that others can also find it.

I have the following JSON response for my REST endpoint:

{
  "response": {
    "status": 200,
    "startRow": 0,
    "endRow": 1,
    "totalRows": 1,
    "next": "",
    "data": {
      "id": "workflow-1",
      "name": "SampleWorkflow",
      "tasks": [
        {
          "id": "task-0",
          "name": "AWX",
          "triggered_by": ["task-5"]
        },
        {
          "id": "task-1",
          "name": "BrainStorming",
          "triggered_by": ["task-2", "task-5"]
        },
        {
          "id": "task-2",
          "name": "OnHold",
          "triggered_by": ["task-0", "task-4", "task-7", "task-8", "task9"]
        },
        {
          "id": "task-3",
          "name": "InvestigateSuggestions",
          "triggered_by": ["task-6"]
        },
        {
          "id": "task-4",
          "name": "Mistral",
          "triggered_by": ["task-3"]
        },
        {
          "id": "task-5",
          "name": "Ansible",
          "triggered_by": ["task-3"]
        },
        {
          "id": "task-6",
          "name": "Integration",
          "triggered_by": []
        },
        {
          "id": "task-7",
          "name": "Tower",
          "triggered_by": ["task-5"]
        },
        {
          "id": "task-8",
          "name": "Camunda",
          "triggered_by": ["task-3"]
        },
        {
          "id": "task-9",
          "name": "HungOnMistral",
          "triggered_by": ["task-0", "task-7"]
        },
        {
          "id": "task-10",
          "name": "MistralIsChosen",
          "triggered_by": ["task-1"]
        }
      ]
    }
  }
}

I am using a groovy gpath expression for an extraction as follows:

given()
.when()
.then()
.extract()
.path("response.data.tasks.findAll{ it.triggered_by.contains('task-3') }.name")

which correctly gives me
[Mistral, Ansible, Camunda]

What I am trying to achieve is to find the task names that are triggered by the InvestigateSuggestions task. But I don't know for sure that the taskId I have to pass in to contains() is task-3; I only know its name i.e. InvestigateSuggestions. So I attempt to do:

given()
.when()
.then()
.extract()
.path("response.data.tasks.findAll{ 
    it.triggered_by.contains(response.data.tasks.find{
    it.name.equals('InvestigateSuggestions')}.id) }.name")

which does not work and complains that the parameter "response" was used but not defined.

How do I iterate over the outer collection from inside the findAll closure to find the correct id to pass into contains() ??

Johan Haleby

unread,
Sep 2, 2018, 11:22:14 AM9/2/18
to rest-a...@googlegroups.com
Very good question! I've answered the SO question as well but I'll answer by email as well for reference. Here goes:

You can make use of a dirty secret, the "restAssuredJsonRootObject". This is undocumented (and subject to change although it has never change as far as I can remember in the 7 year+ lifespan of REST Assured).


This would allow you to write:

    given()
    .when()
    .get("http://localhost:8080/workflow-1")
    .then()
    .extract()
    .path("response.data.tasks.findAll{
        it.triggered_by.contains(restAssuredJsonRootObject.response.data.tasks.find{

        it.name.equals('InvestigateSuggestions')}.id) }.name")

If you don't want to use this "hack" then you need to do something similar to what Michael Easter proposed in his answer.

Hope this helps somewhat.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Riyas Valiya

unread,
Sep 4, 2018, 10:53:04 AM9/4/18
to REST assured
Thanks very much. I had already fiddled around the rest-assured source code and found that little nugget and it worked too. I just wanted to know if there is a cleaner or more correct way to do it.

Thanks,
Riyas

Johan Haleby

unread,
Sep 5, 2018, 2:34:57 AM9/5/18
to rest-a...@googlegroups.com
Yeah, as I said on stackoverflow we should probably promote this to a non-hidden feature :)
Message has been deleted

kalpana na

unread,
Jul 24, 2020, 11:01:34 AM7/24/20
to REST assured
Hi Guys,


continuation to your post, i have a query that how can we get "all the names whose triggered_by contains "task-singledigit"?
i mean to say here is get the names of all whose triggered_by contains task-single digit only but it can be anynumber.

i tried many things but did not get success.
Please help me out

Michael Pinnegar

unread,
Jul 24, 2020, 11:04:57 AM7/24/20
to rest-a...@googlegroups.com
Part of the problem with the "groovy snippets" inside of the .path() is that it's hard to tell which object you have ahold of.

Try adding println statements in the groovy like this. This is a super simplified example, but I think you should see the console out from the groovy in your console and you can use that to figure out what part of the response you're getting back.

.path("println(response.data.tasks.id)")


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

kalpana na

unread,
Jul 24, 2020, 12:52:23 PM7/24/20
to REST assured
Hi Michael,
Thanks For acknowledging.

i am trying the same in rest assured and used the below code but getting null as result.
.path("response.data.tasks.findAll{ it.triggered_by.contains('task-%s') }.name")

i tried numrous regex combinations but none has worked. Looking for solution where i can get all result having triggered_by task-1,task-2 etc etc.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-a...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages