Driver command type: 'JSON_OBJECT' coming through as a String

Can someone explain how does the JSON_OBJECT type works for a device driver command?

It's probably easier to show what I'm doing.. in the driver code there's a method, setLocation, that takes a JSON_OBJECT

        command('setLocation', [[name: 'Set Location', type: 'JSON_OBJECT', description: 'blah..']])

Then, in the command definition I'm logging the value and type:

def setLocation (loc) {
    log.info "Location received ${loc}"
    log.info "TYPE: ${loc.class}"

I entered the following in the driver page: {"lat": 80} and here's what's logged:

Location received {"lat": 80}
TYPE: class java.lang.String

I was expecting the variable to be a JSON object -- meaning I could reference loc.lat but as you can see it's a String. Am I missing something? I imagine I could just take in a String and convert it to JSON but was thinking the JSON_OBJECT would do that for me somehow

thanks,
joe

I believe the intent is that is what you have to do, use parseJson() to convert it to a List or Map yourself (as witnessed by the number of apps that blew up with an error related to this method when thermostat drivers that use this data type were corrected a few platform versions ago and some third-party drivers did not follow suit :smiley: ).

An example??

def getStatusHandler(resp, data) {
	if(resp.getStatus() == 200 || resp.getStatus() == 207) {
		def setStatusResult = parseJson(resp.data)
       //... use the List value pairs
    }
}
1 Like

Is that from one of the packages already available in a driver, without needing to do an import?

I have used JsonOutput.toJson(resp.data), but that required an import of groovy.json.JsonOutput.

Yes, parseJson() is available by default in the driver environment.

1 Like

it's the first 3 lines at:

Screenshot 2023-09-02 at 9.01.13 PM

1 Like

My bad, I got it round the wrong way, you are parsing a Json string into a map. In my driver I was wanting to output the Json in a formatted string. Or maybe I am still confused... Not to worry, don't want to hijack the topic any more than I already have.

Sorry I may not have explained the question right but how is JSON_OBJECT different from TEXT in the command argument? It seems both just pass in a string