httpGet and parsing json response

I’m using the following simplified code:

httpGet(‘http://weatheriotbeacon.com/GetMeter.php?NUMBER=12345678’) { resp ->
ref key = resp.data.electric.key
log.debug “${resp.data.electric.key}”

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?

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: 'http://weatheriotbeacon.com/GetMeter.php?NUMBER=12345678',
        contentType: 'application/json'
    ]
    httpGet(params) { resp ->
        def myObj = resp.data.electric   //get the electric object from JSON
        log.debug "The key is: ${myObj.key}" //9
    }

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