Search code examples
jsontcleggdrop

tcl json output to channel and map/loop items?


Im trying to get output from a websocket to eggdrop tcl script.. the websocket part is working and i get output like this:

{
    "action": "insert",
    "row": {
        "id": 7814727,
        "name": "Doom_Squad-Countdown_To_Doomsday_II-WEB-2016-ESG",
        "team": "ESG",
        "cat": "MP3",
        "genre": "",
        "url": "",
        "size": 0,
        "files": 0,
        "preAt": 1493884429,
        "nuke": null
    }
}

How can i loop or map the items? It looks like json to me so i tried the following: This works for another output of the same site, only that output contains [ ].

Above output doesn't, so is it really json?

set asadict [::json::json2dict $body]

lmap item [dict get $asadict row] {
    dict filter $item key name team cat genre size files preAt nuke
}

foreach item [dict get $asadict row]  {
    dict with item {

       set humanReadableDate [clock format $preAt]

    putquick "PRIVMSG $chan :\003\[\0037PRE\003\]\003\[\0033$cat\003\]\003 $name \003\[\00312PRETIME\003\]\003 \002$humanReadableDate\002 \[PRE-INFO\] \[Group: $team\] \[Size: $size MB\] \[Files: $files\] \[Genre: $genre\] \[Nuked: $nuke\]"

    }
}

Solution

  • Well, since there is only one element of row, you don't need to loop on it:

    set asadict [::json::json2dict $body]
    
    set item [dict get $asadict row]
    dict filter $item key name team cat genre size files preAt nuke
    
    dict with item {
       set humanReadableDate [clock format $preAt]
       putquick "PRIVMSG $chan :\003\[\0037PRE\003\]\003\[\0033$cat\003\]\003 $name \003\[\00312PRETIME\003\]\003 \002$humanReadableDate\002 \[PRE-INFO\] \[Group: $team\] \[Size: $size MB\] \[Files: $files\] \[Genre: $genre\] \[Nuked: $nuke\]"
    }
    

    When you were trying to loop on it, you were actually looping through the keys and values each one at a time.