Home > Back-end >  Accesing unknown nested objects in a JSON using GROOVY
Accesing unknown nested objects in a JSON using GROOVY

Time:08-25

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

CodePudding user response:

{ 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]
  • Related