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.

Unable to parse json, I need some help please

Help for those learning Tcl or writing their own scripts.
Post Reply
c
cnecrea
Voice
Posts: 3
Joined: Sat Oct 08, 2022 9:01 am

Unable to parse json, I need some help please

Post by cnecrea »

Hi there,

I am unable to parse json from http://api.ipregistry.co

The json output is like:

Code: Select all

{
"security": {
"is_abuser": false,
"is_attacker": false,
"is_bogon": false,
"is_cloud_provider": false,
"is_proxy": false,
"is_relay": false,
"is_tor": false,
"is_tor_exit": false,
"is_vpn": false,
"is_anonymous": false,
"is_threat": false
}
}
And this is a part of my TCL. Can some one help me with this, please? I'll appreciate a lot

Code: Select all

# json parse
proc getjson {get data} {
   set parse [::json::json2dict $data]
   set return ""
   foreach {name info} $parse {
    if {[string equal -nocase $name $get]} {
      set return $info
      break;
      }
    }
    return $return
    }


# process check proxy
proc proxy_check {ip} {
global getjson
	set link "http://api.ipregistry.co/$ip?key=s1nr112dyxmw3oy9"
	set ipq [http::config -useragent "lynx"]
	set ipq [::http::geturl $link]
	set data [http::data $ipq]
    ::http::cleanup $ipq
    set is_abuser [getjson "is_abuser" $data]
    set is_attacker [getjson "is_attacker" $data]
    set is_bogon [getjson "is_bogon" $data]
    set is_cloud_provider [getjson "is_cloud_provider" $data]
    set is_proxy [getjson "is_proxy" $data]
    set is_relay [getjson "is_relay" $data]
    set is_tor [getjson "is_tor" $data]
    set is_tor_exit [getjson "is_tor_exit" $data]
    set is_vpn [getjson "is_vpn" $data]
    set is_anonymous [getjson "is_anonymous" $data]
    set is_threat [getjson "is_threat" $data]
User avatar
CrazyCat
Revered One
Posts: 1305
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

First, "global" is used for variables, not procedures.

And if you create a dict called parse, you must use the dict utilities:

Code: Select all

proc getjson get data} {
   set parse [::json::json2dict $data]
   if {![dict exists $parse security $get]} {
      return ""
   } else {
      return [dict get $parse security $get]
   }
}
Post Reply