Seeking a simple JSON parse example

I don't know exactly what data and format you need, but you could do something like this:

/*

 */

metadata 
{
    definition( name: "json resp demo", namespace: "tomw", author: "tomw", singleThreaded: true ) 
    {
        command "trace"
    }
}

def trace()
{
    def url = "https://aa.usno.navy.mil/api/seasons?year=2023&tz=-6&dst=true"

    def res = syncop(url)
    parseResp(res)

    asyncop(url)
}

def parseResp(Map res)
{
    def phenom
    ["Equinox", "Solstice"].each
    {
        phenom = res?.data?.findAll { phe -> phe?.phenom == it }
        
        if(phenom)
        {
            phenom.eachWithIndex
            { thisone, idx -> 
                sendEvent(name: it+idx, value: "${thisone.month}:${thisone.day}:${thisone.year}")
            }
            
            //log.debug "${it}: ${phenom}"
        }
    }
    
}

def syncop(url)
{
    def res
    httpGet(url)
    {
        resp->
        log.debug "sync respose: ${resp.data}"
        res = resp.data
    }
    
    return res
}

def asyncop(url)
{
    asynchttpGet('theCallback', [uri: url])
}

def theCallback(resp, data)
{
    if(resp.data)
    {
        def res = new groovy.json.JsonSlurper().parseText(resp.data)
        log.debug "async response: ${res}"
        
        parseResp(res)
    }
}

1 Like