I've moved this project to github. Hopefully this move will allow for better project development, and management while improving community support.
This page will no longer be updated and may be removed eventually.
I really like this script and it has been very useful for my bot that I use on Twitch. However, I seem to be having an issue. The problem is that about 1% of the time, the name returned will either be blank or it will be the name of someone who previously had their name retrieved from the JSON. I use this code for getting the proper display name from Twitch, as IRC has everybody's name in lower case and this script is the only way that I know of of how to get the proper case sensitive display name. Is there something that I could change to get it to work 100% of the time, or is this just a Twitch issue, or something else? I use $twitch_name(name_to_retrieve) to get the proper name. Thanks.
alias twitch_name {
JSONOpen -ud twitch_name https://api.twitch.tv/kraken/channels/ $+ $1
return $json(twitch_name, display_name)
}
this is, most likely, a twitch issue. Though you could most likely try to manually close the json handler at the end of your twitch_name
alias.
Though you can get a user's display name, if you receive message tags, (CAP REQ :twitch.tv/tags
) via $msgtags(display-name).key
at the time of them sending a message
Well, almost immediately after posting my last message, I learned fast the limitations of trying to use the $msgtags. It's a bad idea and only works in specific situations. I did however figure out a basic "workaround" for getting my twitch_name alias to work. I tested this by using "$twitch_name(name1) $twitch_name(name2) $twitch_name(name3) " on the same line with and without the workaround. Without the workaround, it would always display the name for "name1" for each name. With the workaround, it displays everybody's name.alias twitch_name {
if %tn = 1000 { %tn = 0 }
inc %tn
JSONOpen -ud twitch_name $+ %tn https://api.twitch.tv/kraken/channels/ $+ $1
return $json(twitch_name $+ %tn $+ , display_name)
JSONClose twitch_name $+ %tn
}
So I'm really confused here and I can't get it to work. I got a API that returns
{"Mega Man":{"any% (All Stages)":{"video":"http:\/\/www.twitch.tv\/dabigbooi\/v\/3834763","date":"1424862000","id":"pzg7o9ez", snip
Now I want to return the 'any % (All Stages)' data, but I can't find a way to do $json() for it.
$json(game, 0) returns ["Mega Man"] and I can't seem to find how to return the any% (All Stages) part of it.
I'm feeling real dumb. It really bugs me it has no ID to use such as "video" "date" "id" ...
Hope I'm making sense here.
yo so i need a bit of help. so i need to get the value of time and player under each category.
the command would be !wr sm64 120_star. then the script would go to http://www.speedrun.com/api_records.php?game=sm64
then it would parse and find the values for player and time under the category 120 star. but because the game constantly changes i need to be able to read the first value and basely skip over it. but i want the end response to be "the world record for Super Mario 64 is (time) by (player)" but speedrun.com allows for abbreviations such as sm64, but i want the response to have the whole name. and the json does contain the whole name. so in the end i need to get the first value then parse to the time and player and output all 3.
here is my code so far.
on *:TEXT:!wr *:#: {
if (!%comwr) {
set -u1 %comwr 1
JSONOpen -u wr http://www.speedrun.com/api_records.php?game= $+ $replace($2 , _, $chr(32))
msg $chan http://www.speedrun.com/api_records.php?game= $+ $replace($2 , _, $chr(32))
msg $chan test $json(wr, (help needed), $replace($3 , _, $chr(32)))
JSONClose wr
else {
msg $chan Sorry $nick $+ , no one is playing $2-
JSONClose stream
}
}
else {
msg $chan Sorry $nick this command is on cooldown.
}
}
Mkay, that API is not following generally accepted 'best' JSON practices: its using non-static data for item keys; due to this, my JSON parser may not correctly be able to handle this data.
With that said, you may be able to use $JSON().fuzzy
to access the child members assuming that the child members to be accessed have non totally numeric keys:
JSONOpen -u wr http://www.speedrun.com/api_records.php?game= $+ $replace($2 , _, $chr(32))
;; get the first item* in the wr json's root object then get the user specifiec catagory
;; *: In json indexes start at 0 not 1
var %ref = $JSON(wr, 0, $replace($3 , _, $chr(32))).fuzzy
if ($JSONError) {
;; handle error
}
elseif (!%ref) {
;; catagory doesn't exist
}
else {
;; get player and time values
var %player = $JSON(%ref, player)
var %time = $json(%ref, time)
;; DO STUFF HERE WITH %player AND %time
}
I've trying to use the Google URL shortener API: https://developers.google.com/url-shortener/v1/getting_started to shorten URL's before posting them but I cant seem to get a response back. Any and all help would be appreciated.
jsonopen -uw surl https://www.googleapis.com/urlshortener/v1/url?key=[API KEY]
jsonurlmethod surl POST
jsonurlget surl "longUrl": "http://www.google.com"
echo -a $json(surl).UrlResponse
jsonurlclose
var %data = {"longUrl":"http://www.google.com"}
JSONOpen -duw surl https://www.googleapis.com/urlshortener/v1/url?key=[API KEY]
JSONUrlMethod surl POST
JSONUrlHeader surl Content-Type: application/json
JSONUrlHeader surl Content-Length: $len(%data)
JSONUrlGet surl %data
echo -a $JSON(surl).UrlResponse
Hey thanks! That worked with one tweak:
var %data = {"longUrl":"http://www.google.com"}
JSONOpen -duw surl https://www.googleapis.com/urlshortener/v1/url?key=[API KEY]
JSONUrlMethod surl POST
JSONUrlHeader surl Content-Type: application/json
JSONUrlHeader surl Content-Length: $len(%data)
JSONUrlGet surl %data
echo -a $JSON(surl, id)
It does not work when you use jsonopen -ud with a name starting with number. Example:
alias jsontest {
jsonopen -ud 3test http://api.urbandictionary.com/v0/define?term=runescape
echo -ag Json name: $json(3test)
echo -ag Result: $json(3test,list,6,definition)
}
I'm having a bit of a problem. Lets say you have a JSON string like such:
{ "200":[["one","two","three"],[1,2,3]]}
Say "200" is an ID. To get the value "one" you'd need to do this: $json(test, 200,0,1)
But what if you don't KNOW that the ID "200" is going to be there? Is there a way to iterate through the objects without knowing the object name with this script? I'm able to do this in C#, but I can't for the life of me figure out how to do this through this script. (Also, do you intend on adding a ".count" suffix, like Timi's?)
Okay, that sort of works, but I've run into a little bit of a problem, rather than giving a winded explanation, I'll give you a small bit of sample code:
alias fuztest {
jsonopen -d test {"1":["one"],"3":["three"],"2":["two"]}
echo -a > $json(test).error
echo -a Result: $json(test, 0, 0).fuzzypath
echo -a Result: $json(test, 1, 0).fuzzypath
echo -a Result: $json(test, 2, 0).fuzzypath
}
The result is this:
Result: ["1"]["0"]
Result: ["1"]["0"]
Result: ["2"]["0"]
Now, as you can see it skips over "3" entirely. It'd be index'ed in the second spot, but .fuzzy lives up to the name fuzzy and checks to see if there's a key named that first?
Is there any way we can get it to only check for it by index? and is there anyway to see the count of the base? like {id:[],id:[]} would be 2. I know .length works as a .count, but just doing a $json(test).length doesn't work. :x
Mkay, i understand the issue as far as fuzzy 'sort of' working, and will see what I can do to fix it. Currently the script checks to see if the specified entry is a key
and if it exists, returns it as such. If the key
doesn't exist it loops over the object looking for the nTH item. So objects with numerical keys
interfere when trying to access the object by indexes. For this I will most likely have to add another suffix that assumes all keys not wrapped in ""s are to be index. ETA: within a week
As far as .count
vs .length
, currently my .length works for arrays and strings. Are you suggesting it also work for objects? Or am I wrong, in what you are suggesting. If so, what would be the expected behavior of $json(test).length
assuming the input data is from your example above.
Added Link to the current beta(v0.2.x) and its documentation. The link will not change but as bugs are fixed, the content it points to will. Check the version listed with the link against what you have for newer versions.
Please do NOT use the beta release for public works. It is instead, meant for users to test and report back. If you notice a bug with the beta, please specify the version along with what is wrong as a reply to this thread
Change: Added
$JSONVersion
to return the current JSON for mIRC script version
Change: Set JSON for mIRC script version to 0.1.0
Docs: Updated documentation
Fixed typo that may have lead to specified files being deleted when using
/JSONGet
to send a file
Increased timeout to 60seconds in hopes to avoid popup asking to 'end' or 'continue'
Seems like $json({NAME}) doesn't work. Here's an example:
alias jsontest {
jsonopen -ud test http://api.urbandictionary.com/v0/define?term=runescape
echo -ag Json name: $json(test)
echo -ag Result: $json(test,list,6,definition)
}
Result:
Json name:
Result: An [MMORPG](Massively Multipplayer Online Role Playing Game) with about 75,000 people playing at it's peak times. Reached 200,000 subscribers in '05. It has 2 versions. They are called F2P (Free 2 play) and P2P (Pay to play). F2P is fun and free, but with P2P you get tons more map space. RuneScape is commonly abbreviated as RS.
I was wondering if this needs an update... I was trying to get the account ID from world of tanks API but I get a blank return.. Here is the API link for it... https://api.worldoftanks.eu/wot/account/list/?application_id=demo&search=blood_wolf89
I was wondering if this needs an update... I was trying to get the account ID from world of tanks API but I get a blank return.. Here is the API link for it... https://api.worldoftanks.eu/wot/account/list/?application_id=demo&search=blood_wolf89
Updated - Bug Fixes:
Added check if Internal JSON object exists before attempting polyfill
Removed unneeded code from internal json parser
Fixed Failed Web-Request errors being hidden by later errors that result from the failed web-request
Fixed Request Method being case sensitive
Fixed Temp file cleanup not happening
Fixed typo with %LTLError
Fixed JSON handler not closing when error occurs with /JSONOpen even if the -d switch was specified
Im trying to parse JSON returned from the Steam API but its spaced really weirdly and doesnt do the thing. Anyone happen to know a workaround? On the picture you'll see the difference between normal JSON and Steam's JSON. Ive uploaded the full json here: http://pastebin.com/raw.php?i=eSx1acN3
neeeeevermind it wasnt the spacing that was the problem. I didnt realize i had to specify the array when there was only one. instead of $json(steam, response, players, personastate) i had to specify the 0th array like $json(steam, response, players, 0, personastate). Left the post for future peeps who might run into the same troubles.
How would I access an array formatted like this? http://pastebin.com/8tSnU5WV
When I try to use
echo -a $json(gameinfo,game,teamTwo,array,2)
I get a random string of numbers. I'm sure I'm just doing it wrong but I don't quite know how to do it. I saw the examples, but I'm not sure how to apply it to URL get. I'm a newbie be gentle. I'm posting this again, because I accidentally posted it as a reply.
When accessing the json, if you are trying to access a 'node' that has child members, a reference to those child members is returned, instead of a value. To get values from the json as you listed, you'd have to specify each member you want to grab.
This returns a reference to game.teamTwo.array[0]:
alias test_json {
jsonopen -ud test http://pastebin.com/raw.php?i=9H43Gbav
echo -s Rating: $json(test, game, teamTwo, array, 0)
}
This gets teamRating
and pickMode
from game.teamTwo.array[0]
:
alias test_json {
jsonopen -ud test http://pastebin.com/raw.php?i=9H43Gbav
echo -s Rating: $json(test, game, teamTwo, array, 0, teamRating)
echo -s pickMode: $json(test, game, teamTwo, array, 1, pickMode)
}
i know that the mirc has an limitation but some $json data results have more than 4098 characters so the JSON return an error on because is trying to set the %res with more than 4098 so the only way is to read only 4098 characters instead to return an error, and of course the desired results will not be the correct but its not an code fault but mirc's one.
As ^WeSt mentioned, it's not the fault of the code, but mIRC itself gives an error when the data is longer than 4098 characters. I don't think this will destroy the json array, this will only limit the returning string to 4098 characters (correct me if I'm wrong)
The $left may be a temptorary fix until the dev fixes it. I have posted this problem before http://hawkee.com/thread/118182
Well After Watching Farcrada's Issues, i tried to test this code my self. It failed to even return data. So i decided to make a Small Snippet in order to test your JSON Lib on both Secure and Non Secure sites.
I'm Running on Windows 7 Ultimate 64 Bit with OpenSSL Installed. The Code and Results are the following:
alias Start_Json {
set %szGlob 0
echo -a Running JSON Lib. $time(HH:nn:ss mm/dd/yyyy)
echo -a mIRC Version: $version
echo -a OS Version: $os
if ($sslready) {
echo -a SSLReady:3 $sslready
}
else {
echo -a SSLReady:4 Failed
}
set %szUs https://api.twitch.tv/kraken/streams/lcs_pros_in_koreansoloq
set %szUn http://ipinfo.io/8.8.8.8/json
echo -a Test Sites:
echo -a - Secure Site: %szUs
echo -a - Insecure Site: %szUn
sockopen -e szCTestUS $gettok(%szUs,2,47) 443
sockopen szCTestUN $gettok(%szUn,2,47) 80
}
on *:sockopen:szCTest*:{
if (!$sockerr) {
signal szCTest $true $sock($sockname).addr
}
else { signal szCTest $false $sock($sockname).addr }
sockclose $sockname
}
on *:signal:szCTest:{
if ($1 == $true) { inc %szGlob }
else { unset %szGlob | echo -a Test Failed: | echo -a - Error: while connecting to $2 }
if (%szGlob == 2) { echo -a Sites Online Procceeding | unset %szGlob | Run_Json }
}
alias Run_Json {
var %jHandlerUs jH1
var %jHandlerUn jH2
var %szFail
echo -a Opening Connectiong to %szUs with JSONOpen
JSONOpen -u %jHandlerUs %szUs
if (!%JSONError && $json(%jHandlerUs, stream, game)) {
echo -a Success: $v1
}
else {
echo -a Failed: %JSONError
inc %szFail
}
echo -a Closing JSONHandler: %jHandlerUs
JSONClose %jHandlerUs
echo -a Opening Connectiong to %szUn with JSONOpen
JSONOpen -u %jHandlerUn %szUn
if (!%JSONError && $json(%jHandlerUn, ip)) {
echo -a Success: $v1
}
else {
echo -a Failed: %JSONError
inc %szFail
}
echo -a Closing JSONHandler: %jHandlerUn
JSONClose %jHandlerUn
echo -a Test Complete
echo -a Errors Occured: $iif(%szFail,$v1,None)
}
What exactly are you trying to do. No offense but your code is a bit all over the place.
; //echo -a $Twitch_Streaming_Game( <twitch_user> )
; Returns the game the specified user is streaming or $false
alias Twitch_Streaming_Game {
; Create some local variables
var %r, %j = Twitch_Streaming_Game
; Retrieve JSON from twitch
JSONOpen -u %j https://api.twitch.tv/kraken/streams/ $+ $1
; Check that there's no errors, and that data.stream.channel.game has a value
if (!%JSONError && $json(%j, stream, channel, game)) {
; save the data in a return variable
%r = $v1
}
; immediately close the JSON handler
JSONClose %j
; return the result, if there isn't one, return $false
return $iif(%r, $v1, $false)
}
Thank you for this post it has helped a lot. I just have a quick question. I would like to have an output from this ONLY if the result comes back $false. I'm guess its as simple as an "if" statement but Im new to coding, and am not sure what the parameters would be. (if you would even use an if statement) Thank you for your time!
For some reason it never updates and keeps the same information... why?
So I have it go to this: http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=132
Then i use
jsonopen -ud dogetosatcryptsy http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=132
set %dogetosat $json(dogetosatcryptsy,return,markets,DOGE,lasttradeprice)
msg #dogeinfo 1 DOGECOIN = %dogetosat BTC
However, it keeps saying it's at 0.00000040, which it was ages ago. How do I fix this to make it show real-time info?
append something like &nocache= $+ $ticks to the url. windows XMLHTTPRequest caches results so to get around that alter the url :)
jsonopen -ud dogetosatcryptsy http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=132&nocache= $+ $ticks
set %dogetosat $json(dogetosatcryptsy,return,markets,DOGE,lasttradeprice)
msg #dogeinfo 1 DOGECOIN = %dogetosat BTC
I don't understand the question