Please Disable Ad-Block To View This Website.

If you block ads, this site can not survive!

Ads are very minimal for registered users. If you don't have an account please register now!

 Full Scripts
 MTS Themes
 File Queue
 Download mIRC
 Top Downloads
 Submit Form

Site Search

Link to us!

Home | Comments:
Average Rating:   10   $json by Timi
This identifier can read any value from valid JSON.
The identifier uses the MSScriptControl.ScriptControl object to read from the JSON.

More information such as usage can be found in the comments at the top of the snippet.

Submitted Review Author's Updates
If you are coming from a web development background you will instantly understand what json stands for. The JSON format has become quite popular in the web-dev world however people enjoy using their favorite format and have ported it to C++ and others areas.

Well here is a version that is fantastic and works quite well with mIRC. I can definitely right away picture some nice features to be added to this snippet i think it will become quite useful to many of you.

Very well done.

Reviewed by codemastr_
2010/9/6 Update-
As it turns out, URLs weren't being encoded. URLs now must be encoded before using $json, and $json.enc and $json.enccomponent are supplied for this task.

/jsonclearcache now clears the cache of a specific URL.

Quotation marks (") are now replaced with \u0022.

2010/4/13 Update-

Has the security changes included in the pastebin link

Read JSON from file and URL.

.count property to count the amount of items in an array/list. (will also count the length of other things, such as strings, so be sure the target is actually an array).

Comes with $gws, a Google Web Search identifier.


  Mode:    Create New Post

TimiSep 11, 2010 11:43AM
Note: I left an echo -a in alias jsonclearcache from debugging. It should be changed to noop

Text edited by author on Sep 11, 2010 @ 11:44AM

TimiMar 12, 2010 6:35PM
An update has been released.

Fixed security hole in direct JSON input. (Thanks NiLon :D)
Added ability to read from files and URLs. URL reading does not require mIRC sockets.
Added example usage of URL reading with $gws, an identifier for retrieving information from Google Web Search.

Edit: I totally forgot to validate input (again), this time with URL and file input.
Here's a link to the snippet until I can update it again.

Text edited by author on Mar 13, 2010 @ 1:50PM

NiLonMar 15, 2010 3:44AM
Not bad at all. I will crawl this with more time but looks promising.
Few suggestions how to develop this will raise already.

First of currently it's doing only GET requests, this could also allow POST, PUT, DELETE.
On post, it should allow user to give also content for request. Doesn't need to be anything fancy but you get the idea.

- Possible to add custom headers for request (cookies, http auth etc.)

More to look but not that needy:
- Viewable struct of data (when using url), could also just be pure JSON presented
- HTTPS support
- Save result (file/hash table)
-- I noticed you added file support, good, but it's currently lacking this save option

This is one of greatest work around here, with some love it will be even better. Thanks.

Text edited by author on Mar 15, 2010 @ 3:48AM

NiLonMar 15, 2010 7:23PM
I noticed that cache can be evil thing. It could be nice to actually be able to clear cache somehow. Tried to run jsonclearcache but it didn't do a frag. Adding $ticks or similar to request works, but sometimes it's simply impossible and I would not like to use such ugly "hack". One simply solution could be just applying $json(%url).nocache syntax and it will bypass if that checks cache.

TimiMar 17, 2010 7:04PM
I like the idea of adding POST, and I'll definitely add it. I probably won't add other requests since they aren't really used for JSON retrieval.
I also will definitely add custom headers since they'll be handy.
The only thing is that adding all of these features will complicate syntax. I suppose this is a great opportunity to use a different syntax for accessing JSON like you suggested before, although I probably won't make it custom.

As for https, I totally forgot the XMLHTTP thing already supports it. All that needs to be done is adding || https://* iswm $1.

I was going to add /json to write JSON to file, but I was working on other things, so I decided to release it without the write feature. I'll add it for the next update.

Finally, since I was too busy, I rushed through the caching and made it majorly annoying. /jsonclearcache worked for me (I also typoed the name of that alias in the top documentation), but it clears the whole cache which isn't necessarily desired. I'll add a feature to clear a specific entry in the cache and add the nocache property as you suggested. As for result caching, I still probably won't do this since I'm trying to keep everything managed in COM right now.

NiLonMar 18, 2010 5:19AM
Timi said:

I like the idea of adding POST, and I'll definitely add it. I probably won't add other requests since they aren't really used for JSON retrieval.
Some webservices are designed to support RESTful requests that require you to use PUT and DELETE. This is not uncommon at all. For reference

NiLonMar 3, 2010 1:09AM
First of there is one heavy security risk.

var %j = "foo";x="bar"
echo -a $json(%j,)

This will echo "bar" as injected. Please note that this is only setting variable, it could potentially run any code you like.
If this is going to be used to retive data from untrusted source, this is serious problem.
Until proper fix this could be used

var %json = "foo";x="bar"
var %json = $replace(%json,;,\u003b)

What you are going to do with little bit larger sets? I know that parser can handle those without any problem, but mirc got limits.

Some tweaking ideas:

There is missing count method. Currently it's kind of very hard to get count of list items for example [1,2,null,3].

Alternative syntax could be $json(<data>,<namespace> [, separator])
instead of using $json(%j,foo,2) could be used like $json(%j,foo.2)

var %j = {"foo":[1,2,3]}
noop $json(%j, foo.2)
noop $json(%j, foo.2, $chr(46))

Then there's this

var %j = "foo"
noop $json(%j,)

Comma needs to be added to get that string out of it.

It could push data to hash table or similar. Didn't do benchmark but I could think that without parsing json every time again you could save some time.

Text edited by author on Mar 3, 2010 @ 2:45AM

TimiMar 3, 2010 8:34PM
Thanks for the suggestions and stuffs :D

I hadn't checked it for injection since I rather quickly made this, so I appreciate you bringing this up. I'll be sure to have your fix implemented when I finish the rest of the updates.

As for mIRC's character limit, I do have a solution for this. Since most uses for JSON are for AJAX, I added JavaScript code that will load the JSON from a url all without any mIRC interaction, except where the results are cached so the data doesn't have to be redownloaded. I don't think I'll implement a cache for specific queries since I doubt there will be any significant improvement in time.

Also, I didn't add a count option since, in most cases, the JSON structure is already known. However, since it'll be no worse than convenient, I'll go ahead and add it anyway :D

I don't see a benefit coming from using custom syntax for JSON queries, so I don't think I'll implement that.

To simplify the process of caching JSON from URLs, I am considering keeping one COM open where all JSON queries will go to. The cache would also be kept there which would remove mIRC interaction.

Finally, I'm also experimenting with mIRC data storage with JSON. Being more flexible than INIs and easier to implement than XML, this would be an interesting new way to store data.

NiLonMar 3, 2010 11:27PM
By count I mean this

var %json = {"list":[1,2,null,3,4]}
var %count = $json(%json,list).count, %i = 0
while (%i < %count) {
  echo -a Value: $json(%json,list,%i)
  inc %i

This would iterate whole list like it should.

var %json = {"list":[1,2,null,3,4]}
var %i = 0
while ($json(%json,list,%i)) {
  echo -a Value: $v1
  inc %i

This currently working example would only give you first 2 items.
Even if you know "struct" of json, you can't possible know number of items and so on. This is so called critical feature as there is no way to retive whole list by doing $json(%json,list).

TimiMar 4, 2010 3:50PM
The number of items in a list is often already known, such as in Google APIs, so ideally, the amount of items would be known beforehand. As I said before, I'll be adding count anyway.

I'll be adding the count feature by using the .length property of lists in JavaScript.

NiLonMar 11, 2010 8:25AM
If someone wanna already get that count fix, here is how:

if ($prop == count) var %str = %str $+ .length

var %j = [1,2,null,3]
echo -a $json(%j,).count
;return 4

Text edited by author on Mar 11, 2010 @ 8:27AM

codemastr_Mar 1, 2010 12:53AM
Rating:     10Good Work do you have more useful snippets?

TimiMar 2, 2010 6:10PM
I've been working on a Google search identifier snippet that uses $json in order to demonstrate its powerful use in Google APIs. I'm just ensuring there aren't any bugs since using sockets in that way is a bit of a pain :<

I have added an improvement to the identifier so that data is taken from http urls automatically. Also, an example google web search identifier is also included. I'll upload the update on the 5th when the update limit has expired.
I'll also add other snippets both relating to JSON and not later.

Text edited by author on Mar 2, 2010 @ 8:30PM

Create New Post

You must be logged in to post messages.