Groovy JSON/GPath query

WXB13 picture WXB13 · Sep 12, 2012 · Viewed 16.1k times · Source

Given the following JSON, I would like to extract the postal_code (either long_name or short_name). I've used JsonSlurper to ingest it into a variable and have tried various queries using find/contains/etc. to grab the node that has "postal_code" in its "types" but haven't been able to figure it out. Any help is greatly appreciated.

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Jefferson Ave",
               "short_name" : "Jefferson Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "North Newport News",
               "short_name" : "North Newport News",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Newport News",
               "short_name" : "Newport News",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Virginia",
               "short_name" : "VA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "23608",
               "short_name" : "23608",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Jefferson Ave & Denbigh Blvd, Newport News, VA 23608, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.13852930,
               "lng" : -76.52013079999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.13987828029151,
                  "lng" : -76.51878181970848
               },
               "southwest" : {
                  "lat" : 37.13718031970851,
                  "lng" : -76.52147978029149
               }
            }
         },
         "types" : [ "intersection" ]
      }
   ],
   "status" : "OK"
}

Answer

John Wagenleitner picture John Wagenleitner · Sep 12, 2012

The following should find the node with a postal_code type. If the results or address_components ever have multiple list items you would have to adjust accordingly by replacing the indexed access with some iteration, but hopefully this helps.

import groovy.json.*

def text = '''
{
   "results" : [
<omitted rest to save space>
....
}
'''

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

def theNode = json.results[0]
                  .address_components
                  .find { it.types[0] == 'postal_code' }

assert '23608' == theNode.long_name