0

I have this json link, https://predb.ovh/api/v1/?q=@name%20IfIca.Icssssy

The returned JSON is

{
    "status": "success",
    "message": "",
    "data": {
        "rowCount": 0,
        "rows": [],
        "offset": 0,
        "reqCount": 20,
        "total": 0,
        "time": 0.003080273
    }
}

The output status and message working. When rowCount is also in the output, I get an error:

Tcl error : can't read "rowCount": no such variable

bind pub "-|-" !search pub:test
proc pub:test { nick host handle channel arg } {

    set name [lindex $arg 0]
    set tok [http::geturl "https://predb.ovh/api/v1/?q=@name%20$name"]

    set aadata [json::json2dict [http::data $tok]]
    http::cleanup $tok  
    dict with aadata {
        putnow "PRIVMSG $channel :status $status"
        putnow "PRIVMSG $channel :rowCount $rowCount"
    }

}
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
Mike Shiwa
  • 71
  • 6
  • Cannot reproduce the error. Also, the json contains the key `data`, so you may want to use a different dict name or map this key to a different name. You do have `name` not properly set however, there should be a space between `name` and `[lindex ...`. Maybe that's what causing the issue, because then, the json is different and doesn't have `rowCount`. – Jerry Feb 17 '19 at 11:57
  • i have the same error when i replace data with aadata; set aadata [json::json2dict [http::data $tok]] , dict with aadata { – Mike Shiwa Feb 17 '19 at 12:00
  • Did you also add the space? I was still editing my comment earlier above, sorry. – Jerry Feb 17 '19 at 12:07
  • i have seen now, name is fixed, copy failr. how man you with lindex? set rowCount [lindex $aadata 1] so? then is output "rowCount success" and with set rowCount [lindex $aadata 5] is output empty – Mike Shiwa Feb 17 '19 at 13:13

1 Answers1

1

The problem is that the key rowCount is not directly under the aadata dict, it resides in a sub-dict.

If you want to use dict with you'll have to do

dict with aadata {
    putnow "PRIVMSG $channel :status $status"
    dict with data {
        putnow "PRIVMSG $channel :rowCount $rowCount"
    }
}

or, simpler:

putnow "PRIVMSG $channel :status [dict get $aadata status]"
putnow "PRIVMSG $channel :rowCount [dict get $aadata data rowCount]"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352