How to reference parent names in JSON response (with Groovy)

359 views Asked by At

I have the following JSON response in SoapUI

{
   "formatted":    {
      "line1": "14/8 QUAY STREET",
      "line2": "14/8 QUAY STREET"

   },
   "structure":    {
      "level": null      
   },
   "location":    {
      "nzX": 1758749.75300025      
   },
   "references":    {
      "aid": "A1003467096"    
   }
 }

I want the following as the output

formatted, structure, location and references.

I am using Json slurper but i am not able to get all the parent element names.

How do i do it using JSON slurper in groovy.

1

There are 1 answers

2
Michael Easter On BEST ANSWER

Assuming the JSON is in a string s, consider the following which illustrates getting the top level keys:

import groovy.json.* 

def json = new JsonSlurper().parseText(s)

def parentNames = json.keySet()

parentNames.each { println it }