How to get relative path using Groovy Gpath in json?

1k Views Asked by At

I am using rest assured with Serenity BDD and using Groovy Gpath to navigate thru JSON. Now, I have a scenario like where I need to get the Cost values more than 2 with below JSON. is there any way to use a relative path to navigate using GPath with JSON.

{
"Cadbury": {
    "Specs": {
        "Name": "5 Star",
        "Cost": "2.99"
    }
},
"Nestle": {
    "Specs": {
        "Name": "BarOne",
        "Cost": "1.99"
    }
},
"Arnotts": {
    "Specs": {
        "Name": "TimTam",
        "Cost": "4.99"
    }
},
"Milo": {
    "Specs": {
        "Name": "BarMilo",
        "Cost": "3.99"
    }
}

}

I have found that depthFirst() or ** and breadthFirst() or * traversal can be used with XML but it didn't work with JSON.

Can someone please throw light on this.. Thanks

1

There are 1 best solutions below

0
Ravi On BEST ANSWER

It worked with below query,

collect{it.value.Specs}.findAll{it.Cost > '2.00'}.Cost