-1

so I have been stuck with this problem for quite some time now.

I'm working with some data provided as JSON and retrieved via WSLITE using groovy. So far handling the json structur and finding data was no problem since the webservice response provided by WSLITE is retuned as an instance of JsonSlurper. I now came across some cases where the structure that json response varies depending on what I query for.

Structure looks like this:

{
    "result":[ 

    ...
    "someId": [...]
    "somethingElse": [...]
    ...

    "variants":{
            "0123":{
                "description": "somethingsomething",
                ...,
                ...,

                "subVariant":{
                    "1234001":{
                        name: "somename",
                        ...
                    }
                }   
            },

            "4567":{
                "description": "somethingsomething",
                ...,
                ...,

                "subVariant":{
                    "4567001":{
                        name: "somename"
                        ...
                        ...
                    }
                }
            }
    }
    ]
}

As you can see, the variants node is an object holding another nested object which holds even more nested objects. The problem here is that the variants node sometimes holds more than one OBJECT but always at least one. Same vor the subVariant node. Furthermore I do not now the name of the (numeric) nodes in advance. hence i have to build the path dinamically.

Since Json is handled as maps, lists and arrays in groovy I thought I was able to do this:

def js = JsonSlurper().parseText(jsonReponse)

println js.result.variants[0].subVariant[0].name  //js.result.variants.getClass() prints class java.util.ArrayList

but that returns null

while accessing the data statically like this:

println "js.result.variants."0123".subVariant."1234001".name 

works just fine.

Just pinting the variants like this

println js.result.variants

also works fine. it prints the first (and only) content of the whole variants tree.

My question is: Why is that?

It kind of seems like i can not address nexted objects by index... am i right? how else would i got about that?

thanks in advance.

Christoph Zabinski
  • 327
  • 2
  • 5
  • 17

1 Answers1

0

Well the reason you cant access it using [0] is simply because "variants": { ... isnt an array, that would look like this: "variants" :[ ...

So your variants is an object with a variable number of attributes, which is basically a named map. However, if you dont know the entry names, you can iterate through these using .each, so you could do something like js.result.variants.each{key, val -> println val.description}

And something similar for the subvariant

OsaSoft
  • 589
  • 5
  • 19
  • The thing is 'js.result.variants.getClass()' reveals that it indeed is an java.util.ArrayList – Christoph Zabinski Aug 16 '16 at 07:15
  • can you put the exact json youre getting? Because this seems like its not that, since you even have some json errors in there (eg. name in subvariant not being in quotes) – OsaSoft Aug 16 '16 at 10:57