HttpGet responses not in Json format

Hi.
I’ve managed after much work, to get a data capture from my solar device into Hubitat Code but I can’t get it to format it in json. It comes out like this:-

{result={ownerUser={bindRelation=2, email=Darenkeates@yahoo.co.uk, isAccept=1, isActived=1, isThirdparty=0, isVerify=0, lastLogin=1593360548000, localeId=2, nickName=Keates, etc.....

I have used content type as application/json but still no joy, any ideas please?

That looks like a Json structure represented as a map, which is how the Hubitat http methods will autoparse responses when the contentType is Json.

https://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html

If you try to access the contents of the Json response using groovy map operations does it work? Like: log.debug "${response.result.ownerUser.email}"

If not, what are you trying to do with the data so that we can give better advice on how to get it into the right form?

As @tomw mentioned, you may be able to use it directly. Also, I have found this site useful for checking out json responses for formatting or looking at large responses for relevant data:
http://jsonviewer.stack.hu/

The groovy map operations don’t work on this format and it want to extract the values to use in a device.
I haven’t used jsonslurper before but will take a look at your link. Thanks.

I’ve kept a copy of that site previously as it’s super helpful thanks.

So I’m not clever enough to fully understand how to use the slurper. When I use If I use textParser as below:-

def slurper = new JsonSlurper()
def result = slurper.textParser(response)

Then I get an error -
Preformatted text dev:6102020-10-19 08:21:15.780 warnError: No signature of method: groovy.json.JsonSlurper.textParser() is applicable for argument types: (groovyx.net.http.HttpResponseDecorator) values: [groovyx.net.http.HttpResponseDecorator@13f0ac6]Preformatted text

Is there some other part of the slurper I should be trying.

That is curious, since it looked similar to traces of pre-parsed Json maps that I have seen in my drivers. Can you give a snippet of code and log/error output to show how exactly it was not working? It may help narrow down some of the guessing as to what is going wrong.

Here is an example for how it might work (shown with an actual http operation for context):

def traceRandomCode()
{
    def body= '''{"result":{"ownerUser":{"bindRelation":2, "email":"somebody@gmail.com"}, "isAccept":1}}'''
    
    def slurper = new groovy.json.JsonSlurper()
    log.debug "body as json = ${slurper.parseText(body)}"
    
    def params = ['uri': "https://postman-echo.com/post", 'body': body, 'requestContentType': "application/json"]
    try
    {
        httpPost(params)
        { resp ->
            if (resp.data)
            {
                log.debug "resp.data = ${resp.data}"
                log.debug "resp.data.json.result.ownerUser = ${resp.data.json.result.ownerUser}"
                
                return resp.data
            }
        }
    }
    catch (Exception e)
    {
        log.warn "failed: ${e.message}"
    }
}

It would be easier to guess what is going on (and give suggestions to resolve it) if you could include some code snippets.

1 Like

I'm very grateful and i think I've cracked it, cheers for the suggestions.

Great! Could you share some code and what the missing pieces were to get it working? I'm genuinely curious, and it may be useful to others with similar challenges.

I certainly will. My intention is to publish the solution shortly, I just need to polish it a bit.

1 Like

Here is my code for ginlong data