My Solution For Logging JSON From HTTP Response

For some time I have struggled with trying to decipher some of the more lengthy JSON I can have returned from the Mitsubishi thermostat platforms, particularly KumoCloud in the US and MelCloud in Europe. When I would output the HTTP response to the HE logs it is outputting a map or list I think, so is not formatted as an actual JSON string.

I finally stumbled across this solution, so thought I would share. Every chance this or other solutions have been posted in the past, so happy to hear other options as well.

I start by importing the JsonOutput library.

import groovy.json.JsonOutput;

Then in my driver, the line
debugLog("retrieveChildACUnits_MelCloud: Response = ${JsonOutput.toJson(resp.data)}");

in the code snipped below makes use of this library, outputting a formatted string.

def retrieveChildACUnits_MELCloud()
{
    //Retrieves current list of ac units
    
    def unitsList  = []
    def unitDetail = [:]
    
    def getParams = [
        uri: "${getBaseURL()}/Mitsubishi.Wifi.Client/User/ListDevices",
        headers: getStandardHTTPHeaders_MELCloud("no"),
        contentType: getStandardHTTPContentType_MELCloud(),
        body : "{ }"
	]
    
    debugLog("retrieveChildACUnits_MelCloud: Body = { }, Headers = ${headers}")
	try {
        
        httpGet(getParams) { resp -> 
            debugLog("retrieveChildACUnits_MelCloud: Response = ${JsonOutput.toJson(resp.data)}");
            resp?.data?.Structure?.Devices?.each { unit -> // Each Device
                                      
                                      unitDetail = [unitId   : "${unit.DeviceID}".replace("[","").replace("]",""),
                                                    unitName : "${unit.DeviceName}".replace("[","").replace("]","")
                                                   ]
                                      unitsList.add(unitDetail)
                
                                  } //End of each unit
                           } // End of response (resp)
    }  // End of Try 
	catch (Exception e) {
        log.error "retrieveChildACUnits_MelCloud: Unable to query ${getPlatform()}: ${e}"
	}
    return unitsList
}

I then use the Json Viewer plugin in Notepad++ to get a more hierarchical output, but I am sure there are plenty of online options as well, if you prefer.

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.