Accesing unknown nested objects in a JSON using GROOVY

I have the following JSON from a response from a random REST request:

{ pages= { 56206384={ title = Siberia } }

how can I extract the element "title", given that the number 56206384 will change with every new request? is there a way to have a regex expression for any number?

already tried: def title = ParsedResponse.query.pages.(*).title

any ideas? would appreciate the help

Upvotes: 0

Views: 56

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27255

{ pages= { 56206384={ title = Siberia } }

how can I extract the element "title", given that the number 56206384 will change with every new request?

If you know the structure will be just like what you have shown, you could do something like this.

import groovy.json.JsonSlurper


def slurper = new JsonSlurper()
def json = slurper.parseText '{ "pages": { "56206384" : { "title":  "Siberia" } }'

def title = json.pages.values().title[0]

Upvotes: 1

Related Questions