egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

json2dict help

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
johne
Voice


Joined: 19 Jul 2005
Posts: 29

PostPosted: Wed Feb 15, 2012 2:12 pm    Post subject: json2dict help Reply with quote

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
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Feb 15, 2012 3:39 pm    Post subject: Reply with quote

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:
set mydict [json::json2dict $source]
foreach show [dict get $mydict watched] {
  putlog "title: [dict get $show show title]"
}

or
Code:
set mydict [json::json2dict $source]
putlog "title: [dict get [lindex [dict get $mydict watched] 0] show title]"

_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
johne
Voice


Joined: 19 Jul 2005
Posts: 29

PostPosted: Wed Feb 15, 2012 4:53 pm    Post subject: Reply with quote

worked great! thx!
Back to top
View user's profile Send private message
elbandido
Voice


Joined: 01 May 2012
Posts: 4

PostPosted: Tue May 01, 2012 2:11 am    Post subject: dict with list Reply with quote

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

Code:
[{"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?
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue May 01, 2012 9:46 am    Post subject: Reply with quote

@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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
elbandido
Voice


Joined: 01 May 2012
Posts: 4

PostPosted: Tue May 01, 2012 3:45 pm    Post subject: Reply with quote

yes, like this:

Code:
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.
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue May 01, 2012 3:50 pm    Post subject: Reply with quote

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
elbandido
Voice


Joined: 01 May 2012
Posts: 4

PostPosted: Tue May 01, 2012 4:04 pm    Post subject: Reply with quote

Solved:
Code:
[lindex [lindex $json_data 0] 0]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber