0

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!

Nabeel S.
  • 1
  • 1
  • Do you do something like this - https://gist.github.com/wololock/a9b77feee65584c0a5f6fcb2f0d04c48 ? It works for me. – Szymon Stepniak Aug 07 '17 at 18:05
  • Thanks... that works for me too. I think what's causing it to not work is that there's not always an "authors" object (in the actual dataset, it can be "authors" or "publishers", etc, as an example) – Nabeel S. Aug 07 '17 at 18:13
  • Ok, I got it working by adding a metafield that specifies what the different subfields might be. And doing something like `it.type == "author" && it.author.age==45` – Nabeel S. Aug 07 '17 at 18:18
  • Cool, I'm glad you make it :) – Szymon Stepniak Aug 07 '17 at 18:48

1 Answers1

1

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'.

Lex Draven
  • 11
  • 1