AutomateFr33k
AutomateFr33k

Reputation: 602

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

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.

Upvotes: 0

Views: 396

Answers (1)

Michael Easter
Michael Easter

Reputation: 24498

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 }

Upvotes: 2

Related Questions