This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

json2dict help

Help for those learning Tcl or writing their own scripts.
Post Reply
j
johne
Voice
Posts: 29
Joined: Tue Jul 19, 2005 2:24 am

json2dict help

Post by johne »

i'm trying to pull [dict get $data watched show title] from the data below after using json2dict. what am I missing to pull the watched data via dict or is there a better way?

http://pastebin.com/AEk3feYz
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Looking at the json-data shows that the watched-element contains a tcl-list, not a dict. Considder using something along these lines (assuming $source contains the json-source)

Code: Select all

set mydict [json::json2dict $source]
foreach show [dict get $mydict watched] {
  putlog "title: [dict get $show show title]"
}
or

Code: Select all

set mydict [json::json2dict $source]
putlog "title: [dict get [lindex [dict get $mydict watched] 0] show title]"
NML_375
j
johne
Voice
Posts: 29
Joined: Tue Jul 19, 2005 2:24 am

Post by johne »

worked great! thx!
e
elbandido
Voice
Posts: 4
Joined: Tue May 01, 2012 2:07 am

dict with list

Post by elbandido »

I'm trying to extract id or name from this json source:

Code: Select all

[{"id":2020512,"name":"Romper"},{"id":5882783,"name":"Romper amante"},{"id":5630419,"name":"Romper the last"},{"id":5630428,"name":"Romper the Last II"},{"id":5465877,"name":"Romper5"}]
[dict get $json_data id] seems to not working, any help regarding that?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

@elbandido:
First off, did you convert your json-data to a tcl-list/dict collection using the json::json2dict command?
Secondly, your set of data will return a list of dicts, so you'll either have to iterate through each of them using foreach, or select a specific index using lindex before using dict to get the id property.
NML_375
e
elbandido
Voice
Posts: 4
Joined: Tue May 01, 2012 2:07 am

Post by elbandido »

yes, like this:

Code: Select all

package require http
package require json

bind pub * .search search
proc search {nick uhost hand chan argv} {
	variable url0 "xxx"
	set url "${url0}${argv}/1.json?key=xxxxx"
	set json_data [::json::json2dict [::http::data [::http::geturl $url]]]
}
I want to extract first value for "id" from json data above.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

As I said in my previous post, the json-data you posted will translate to a list of dicts... Use lindex or foreach to extract the dict(s) from the list before using the dict-command on it/them.
NML_375
e
elbandido
Voice
Posts: 4
Joined: Tue May 01, 2012 2:07 am

Post by elbandido »

Solved:

Code: Select all

[lindex [lindex $json_data 0] 0]
Post Reply