2

I have JSON that needs to be processed using Groovy. I am pretty sure that the JSON has only one key, with this format:

{ rootKey: [...] }

Where rootKey stands for different values (e.g. "customers", "stores", etc.).

Let's say I used JsonSlurper:

def map = jsonSlurper.parseText(myjson)

How do I obtain that rootKey string?

oikonomiyaki
  • 7,691
  • 15
  • 62
  • 101

1 Answers1

5

You should be able to use keySet method to get the keys which is a list. Since, you mentioned only key, you can use the first element as shown below:

def jsonString = """{
  "rootKey": []
}"""
def json = new groovy.json.JsonSlurper().parseText(jsonString)
println json.keySet()[0]
Rao
  • 20,781
  • 11
  • 57
  • 77