Nabeel S.
Nabeel S.

Reputation: 1

GPath on nested objects

I'm not sure what I'm doing wrong here and I'm having a hell of a time with getting this to work properly. Using this JSON:

{
    "books": [
        {
            "category": "reference",
            "author": {
                "name": "Nigel Rees",
                "age": 45
            },
            "title": "Sayings of the Century",
            "price": 8.95,
            "tax": 7.00
        },
        {
            "category": "reference",
            "author": {
                "name": "Evelyn Waugh",
                "age": 30
            },
            "title": "A cheap book",
            "price": 6.00,
            "tax": 3.00
        }
    ]
}

I'm not able to extract the books where the authors age is 45, for example. I've tried things like the following (with the books document root set)

findAll {it.author.age == 45}
findAll {it.author}.findAll {it.age == 45}
findAll {it.author}*.each {it.age == 45 }

I still get back all of the records that have an age item. It can be any arbitrary object, it might not have an author record, etc. And I want the root book object returned.

I feel like there's something really obvious I'm missing, but the docs seem to only cover one level of key-values. Maybe it doesn't support it?

Thanks!

Upvotes: 0

Views: 1018

Answers (1)

Lex Draven
Lex Draven

Reputation: 11

here is it

books.findAll{it.author.age==45}

It's not working your way (findAll {it.author.age == 45}) because you work from the root, and 'it' variable returns 'books' object, which has no field 'author'.

Upvotes: 1

Related Questions