httpGet and parsing json response

(Allen) #1

I’m using the following simplified code:

httpGet(‘’) { resp ->
ref key =
log.debug “${}”

The output of the httpGet is a json formatted response, however I can’t seem to get the variable “key” from the parsed json. I’ve tried this on other sites and it works fine. The webpage I’m polling is from a PhP mysql query converted to json. I’ve used a json validator to verify the json is constructed correctly. Any ideas why the json won’t parse correctly and allow output of the desired field?

(Joshua Lyon ( Dashboard)) #2

The headers from that URL have the content-type set as text/html.

If you have control over the web-app, try changing the content-type in the headers on the response to application/json

Alternatively, if you don’t have control over the server, you can specific the contentType in your SmartThings code and it should work:

    def params = [
    	uri: '',
        contentType: 'application/json'
    httpGet(params) { resp ->
        def myObj =   //get the electric object from JSON
        log.debug "The key is: ${myObj.key}" //9

(Allen) #3

Thank you, that worked! I changed the content-type in the web-app.