Busy making things: tinycast, github, links, photos, @mc.

Web Services with JSON and Lua

Posted: October 5th, 2006 | Author: | Filed under: Web Services | 6 Comments »

I’m still not sure why but today I wrote a web services client for the Yahoo! Traffic API using JSON and Lua.

According to Wikipedia, Lua is a lightweight scripting language commonly embedded for use as an in-game scripting language. While it is most commonly used in gaming, I think it’s a simple but very powerful little scripting language.

While exploring Lua a bit I stumbled upon the socket library and decided to couple that with a JSON parser. The short and sweet program checks and prints out traffic information for Kansas City, Missouri in about 20 decently commented lines. Here is some example output from the program.

As with many short and sweet scripting language programs, this one relies on a few external libraries: Luasocket and JSON4Lua. The socket library uses Lua’s C bindings to work its magic but JSON4Lua (and many other extensions) are written in pure Lua. I’ve always been a sucker for a good library written in pure Python, and as such I love pure Lua extensions too.

The JSON parser and HTTP library were particularly neat to work with, as was wrapping my head around Lua tables. Here’s the bit that parses the JSON response in to a Lua table:

results = JSON.decode(r)[”ResultSet”][”Result”]

Lua tables are neat in that you can access them in dict or attribute style so the above code can be rewritten as such:

results = JSON.decode(r).ResultSet.Result

If you’d like to read up on Lua a bit more I would suggest checking out the following sites:


6 Comments on “Web Services with JSON and Lua”

  1. 1 Carfield Yim said at 2:28 am on December 22nd, 2006:

    The link of your homepage use xhref instead of href at tag. Look like neither opera, firefox and IE can click…

  2. 2 Matt Croydon said at 5:35 am on December 22nd, 2006:

    Carfield,

    Thanks for teh heads up, it looks like WordPress kept insisting on changing href to xhref when I pasted in. It should be fixed now.

  3. 3 Daniel said at 6:51 am on August 21st, 2007:

    I have to say, that I could not agree with you in 100% regarding ervices with JSON and Lua at Matt Croydon::Postneo, but it’s just my opinion, which could be wrong :)

  4. 4 Mark said at 11:45 pm on October 27th, 2007:

    Take a look at the BarracudaDrive server and the integrated Lua JSON Web Services plugin.

    Server:
    http://barracudaserver.com/doc/Lua/JsonRpc.html

    JavaScript client:
    http://barracudaserver.com/doc/WebServices/JRpcDoc.html

    Lua Server Pages:
    http://barracudaserver.com/products/BarracudaDrive/LuaServerPages.lsp

  5. 5 buy propecia said at 11:04 pm on January 24th, 2011:

    I join. And I have faced it. We can communicate on this theme. Here or in PM.

  6. 6 tory-burch-slipper said at 4:14 am on February 14th, 2011:

    Tory Burch is developing faster and faster.Tory Burch Boots become more and more fashional.More and more people prefer to wear shoes like this.Tory Burch is an attainable, luxury, lifestyle brand defined by classic American sportswear with an eclecti…

    Tory Burch flip flop online sale.For the younger working expat women,look at these tory burch Shoes.these tory burch flip flop are definitely fun,yet the glossy design and round-toe stay these lovely.These tory burch shoes with multiple.New arrivel Tor…