Parsing large JSON response that has different 'device' types

I am new to Groovy and i am looking for a way to parse a JSON response from a service that communicates a number of device types it is controlling. There is common and specific information per device that is communicated back. How can i use e.g. JsonSlurper to get this response structured in maps or something similar? An example of the response is below.

{
“ActTime” : 1436785935,
“ServerTime” : “Jul 13 2015 13:12:15”,
“Sunrise” : “05:36:00”,
“Sunset” : “21:56:00”,
“result” : [
{
“BatteryLevel” : 255,
“CustomImage” : 0,
“Data” : “Stopped”,
“Favorite” : 1,
“HardwareID” : 2,
“HardwareName” : “RFXCOM”,
“HaveDimmer” : false,
“HaveGroupCmd” : false,
“HaveTimeout” : false,
“ID” : “000001”,
“Image” : “Light”,
“IsSubDevice” : false,
“LastUpdate” : “2015-07-12 17:44:52”,
“Level” : 0,
“LevelInt” : 0,
“MaxDimLevel” : 0,
“Name” : “Somfy_TV”,
“Notifications” : “false”,
“PlanID” : “0”,
“Protected” : false,
“ShowNotifications” : true,
“SignalLevel” : 7,
“Status” : “Stopped”,
“StrParam1” : “”,
“StrParam2” : “”,
“SubType” : “RFY”,
“SwitchType” : “Blinds”,
“SwitchTypeVal” : 3,
“Timers” : “false”,
“Type” : “RFY”,
“TypeImg” : “blinds”,
“Unit” : 1,
“Used” : 1,
“UsedByCamera” : false,
“XOffset” : “0”,
“YOffset” : “0”,
“idx” : “6”
},
{
“AddjMulti” : 1.0,
“AddjMulti2” : 1.0,
“AddjValue” : 0.0,
“AddjValue2” : 0.0,
“BatteryLevel” : 255,
“CustomImage” : 0,
“Data” : “Off”,
“Favorite” : 1,
“HardwareID” : 2,
“HardwareName” : “RFXCOM”,
“HaveDimmer” : false,
“HaveGroupCmd” : true,
“HaveTimeout” : false,
“ID” : “66”,
“Image” : “Light”,
“IsSubDevice” : false,
“LastUpdate” : “2015-07-12 00:02:33”,
“Level” : 0,
“LevelInt” : 0,
“MaxDimLevel” : 0,
“Name” : “Lamp_Buiten_Achter”,
“Notifications” : “false”,
“PlanID” : “0”,
“Protected” : false,
“ShowNotifications” : true,
“SignalLevel” : 5,
“Status” : “Off”,
“StrParam1” : “”,
“StrParam2” : “”,
“SubType” : “X10”,
“SwitchType” : “On/Off”,
“SwitchTypeVal” : 0,
“Timers” : “false”,
“Type” : “Lighting 1”,
“TypeImg” : “lightbulb”,
“Unit” : 4,
“Used” : 1,
“UsedByCamera” : false,
“XOffset” : “0”,
“YOffset” : “0”,
“idx” : “11”
},
{
“AddjMulti” : 1.0,
“AddjMulti2” : 1.0,
“AddjValue” : 0.0,
“AddjValue2” : 0.0,
“BatteryLevel” : 255,
“CustomImage” : 0,
“Data” : “Off”,
“Favorite” : 1,
“HardwareID” : 2,
“HardwareName” : “RFXCOM”,
“HaveDimmer” : false,
“HaveGroupCmd” : true,
“HaveTimeout” : false,
“ID” : “67”,
“Image” : “Light”,
“IsSubDevice” : false,
“LastUpdate” : “2015-07-12 23:35:27”,
“Level” : 0,
“LevelInt” : 0,
“MaxDimLevel” : 0,
“Name” : “Lamp_Buiten_Hal”,
“Notifications” : “false”,
“PlanID” : “0”,
“Protected” : false,
“ShowNotifications” : true,
“SignalLevel” : 6,
“Status” : “Off”,
“StrParam1” : “”,
“StrParam2” : “”,
“SubType” : “X10”,
“SwitchType” : “On/Off”,
“SwitchTypeVal” : 0,
“Timers” : “false”,
“Type” : “Lighting 1”,
“TypeImg” : “lightbulb”,
“Unit” : 1,
“Used” : 1,
“UsedByCamera” : false,
“XOffset” : “0”,
“YOffset” : “0”,
“idx” : “12”
}
],
“status” : “OK”,
“title” : “Devices”
}

Thanks for your help!

1 Like

If you’re POSTing json into a REST/oauth endpoint, it will already be parsed into request.JSON

You then access fields as desired. So

def resarray = request.JSON?result

// now just iterate over resarray
resarray.each {
switch (it.Type) {
case “Lighting 1”:
break
case “RFY”:
break
/// etc…
}
}

3 Likes

Sorry John…not POSTing but doing a httpGet…and i am missing (probably something simple)…
code in this piece:

try {
httpGet(params2) { resp ->
if (resp.data)
{
log.debug "Response Data = ${resp.data}"
log.debug “Response Status = ${resp.status}”
// Parse the response
def dList = ??? what do i need if it is a httpGet???

            dList.each 
            {
                switch (it.HardwareName)

Is your service setting content type to application/json?

That is what i saw in the reponse

Check out the parse function