how to use json library with lua

JSON (JavaScript Object Notation) is a lightweight library used to serialize (encode) and deserialize (decode) lua tables, to or from, a json format.
Inside this json file you'll find an object, which is very similar to the lua tables, also their syntax are very similar as well.
I show you an example.

This is how it looks in lua:

local player = {
    name = "John", 
    lastName = "Quick", 
    age = 32, 
    country = "US"

And this is how it looks in a json format:

var player = {

As you can see both two syntax have a pair of key-value in there, but between these two, there is an " = " sign for lua and a " : " sign for json.
In the json format there is also another important detail that changes from the lua syntax, which is string key at left of the colon sign.

Json also supports a more complex syntax which could include arrays (in lua they are called numeric table).
Let's first see the lua syntax:

local players = {
    {name = "John", lastName = "Quick", age = 32, country = "US"}, 
    {name = "Peter", lastName = "Quarck", age = 24, country = "EN"}, 
    {name = "Johan", lastName = "Querck", age = 32, country = "FR"}

The json version of this table would be like this:

var players = [
    {"name":"John", "lastName":"Quick", "age":32, "country":"US"}, 
    {"name":"Peter", "lastName":"Quarck", "age":24, "country":"EN"}, 
    {"name":"Johan", "lastName":"Querck", "age":32, "country":"FR"}

As you can see the syntax is very understandable.

How to install JSON on your machine?

Oh well, this process is very simple, you have to launch a couple of commands from your terminal and you'll have json installed.

On MacOS:

$ brew update 
$ brew install luarocks
$ brew luarocks install json

On Linux Debian Based:

$ sudo apt-get update
$ sudo apt-get install luarocks
$ sudo luarocks install json

On Linux Arch Based:

$ sudo pacman -Syu
$ sudo pacman -S luarocks
$ sudo luarocks install json

Let's go into the code!

Now start up your text editor and create a new file, i will call it json.lua.
Basically we are going to cover two function very basic, which i mentioned at thetopof this article, which are the encode and decode function.
By considering the previous lua variables, copy the player table inside your file and then encode it.

-- Declare a table/object
local player = {
    name = "John", 
    lastName = "Quick", 
    age = 32, 
    country = "US"

-- Require json library
local json = require "json"

-- Declare an encoded variable
local encoded = json.encode(player)

-- Print the encoded value

Now to see this in action, save this file then open your terminal or console and type, from inside where this file is located:

$ lua json.lua

If this command throws you an error message, is because you have not installed lua on your machine so go take a look at thispageand then come back and try again.
otherwise this output would be generated:


As you can see this format is a json object which the encode() function has built.

Now let's have a de-serialize of this object notation by using the decode() function.
Continuing with the work done previoulsy, type in this statements:

-- Decode the object you have encoded
local decoded = json.decode(encoded)

-- Print the decoded value

Now if you save and run this file you will have a table referred to the decoded data:

table: 0x555aa21c2810

However, this output is really generic... it tell us that the decoded value of "decoded" returns a lua table and that's fine. But there it is another way to achieve these decoded property:

-- Print all the values inside decoded
for k, v in pairs(decoded) do
    print(k , v)

Save, run the file and an output like this should appear:

name        John
lastName    Quick
age         32
country     US
lua json encode decode
Expand your knowledge about this topic