Help parsing JSON


(Ben Vincent) #1

Hi All,

I’m struggling to parse some JSON correctly. Here’s a typical blob:

[{“mode”:“off”,“LastUpdated”:“2016-10-08 19:53:27Z”,“Info”: [“Info1”, “Info2”, “Info3”], “Zones”: [{“ZoneID”:“52”,“ZoneType”:“M”,“ZoneSubType”:"",“ZoneStatus”:“Secure”,“LastUpdated”:“2016-10-08 19:53:27Z”},{“ZoneID”:“3”,“ZoneType”:“D”,“ZoneSubType”:"",“ZoneStatus”:“Not Ready”,“LastUpdated”:“2016-10-08 19:53:27Z”},{“ZoneID”:“123”,“ZoneType”:“D”,“ZoneSubType”:"",“ZoneStatus”:“Not Ready”,“LastUpdated”:“2016-10-08 19:53:27Z”}]}]

The code I’m trying is:

def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(params.command)

log.debug "Object: ${object}"

log.debug "Mode: ${object.mode}"

log.debug "Last Updated: ${object.LastUpdated}"

object.Info.each {
	log.debug "Info: ${it}"
}

object.Zones.each {
	log.debug "Zone ${it.ZoneID} of type ${it.ZoneType} changed to ${it.ZoneStatus} at ${it.DateUpdated}"
}

What this returns is:

Zone [52, 3, 123] of type [M, D, D] changed to [Secure, Not Ready, Not Ready] at [null, null, null]

[Info1, Info2, Info3]

Updated: [2016-10-08 19:53:27Z]

Mode: [off]

But I was expecting Zones to look like this:

Zone [52] etc…
Zone [3] etc…
Zone [123] etc…

Any advice appreciated.