Web Services with JSON and Lua


Posted

in


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:

Comments

6 responses to “Web Services with JSON and Lua”

  1. Carfield Yim Avatar

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

  2. Matt Croydon Avatar

    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. Daniel Avatar

    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. Mark Avatar
    Mark

    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. buy propecia Avatar

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

  6. 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…