0

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.

Michael Easter
  • 23,733
  • 7
  • 76
  • 107
AutomateFr33k
  • 562
  • 2
  • 8
  • 26

1 Answers1

2

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 }
Michael Easter
  • 23,733
  • 7
  • 76
  • 107
  • i don't wan't to assert anything. Say for example if i do json.Size(). It returns me 4 because there are 4 parent nodes(formatted, structure, location and refrences). Now i want the names of those 4 parent nodes. They can be anything and not necessarily the ones that i have mentioned in my example. – AutomateFr33k Dec 06 '17 at 02:54
  • I have updated to show getting the top level keys and how to reference keys like a map. The assertion is merely an illustration. – Michael Easter Dec 06 '17 at 02:55