Updated June 12, 2023
Introduction to Lua JSON
JSON stands for Javascript Object Notation, it works in the same way like any other programming language. JSON are very light weight and store anything inside them, mainly JSON in lua or any other programming language is used to transfer data from server to web pages and vice versa. Inside the JSON in lua we can store anything like objects, array etc. JSON have specific format which we need to follow if we want to parse or deparse our object to Json otherwise we will receive parsing expectation at runtime. JSON always starts with a ‘{‘ curly braces and ends with a ‘}’ curly braces in lua or any other programming language.
Syntax:
To use JSON in lua we have to install external library using Lua rocks, this helps us to manage the library in lua.
local variable_name = '{"key":"value"}'
As you can see in the above lines of syntax we are defining JSON by using ‘{}’ inside this we are defining key and value it can take any object inside the JSON.
Example:
Code:
local demojson = '{"key1 ":"hello"}'
As you can see in the above lines we are defining the key and value in JSON.
How JSON Works in Lua?
As we already seen JSON is used to store data, it can store any kind of data inside it. To use JSON in lua we have to install library using Luarocks. We can follow some command which is used to install the required packages. First we have to install the Luarocks then after that we can install luajson which provide us few methods to handle JSON lua.
Follow below steps to get started with JSON in lua:
1. sudo apt install luarocks: To install luarocks we can follow above steps this will applicable for ubuntu. For Mac OS follow the below command s mentioned.
2. brew install luarocks: This will install the required library for lua to handle JSON.
After following and installing the luarocks on your machine now we will required luajson library, it is also very easy to install like luarocks. We have to execute some commands specific to your operating system.
Below find the commands to start with:
1. For windows: If you are windows user and want to install luajson then follow the below command:
luarocks install lunajson
For this to work we should have luarocks already installed in our machine.
2. To install it on Linux or Mac: To install luajson on your machine make user you have luarocks already installed because it is responsible to manage the lua library for us.
Follow below command to install it on mac or linux:
sudo luarocks install lunajson
Once you setup your libarray or done with the installing of library on your machine then, now we have to make few more configuration in order to use this library inside our program in lua.
- Set path: We have to give the path of library into the program we are using this. If you are ubuntu user we have default path which you can use that is: /usr/local/share/lua/5.veriosn/ After this configuration only we will be able to use the methods from lunajson library to handle the json. Luajson library provide us encoding and decoding mechanism by which we can encode and decode our object into JSON in lua.
Let’s take an look at its syntax and how to use this while program in lua:
To use any of the method from luajson fist we have to import the package into our program.
After that we can assign a local or custom name to the luajson library see syntax below:
name_of_libarary = require 'lunajson'
In the above code we are using ‘require’ keyword to import or use the luajson library into the program, after this line we will be able to use the encoding and decoding in our program.
1. Encode: Encode method is used to convert the normal object into the form of json in lua. We can call this method on the library imported name.
Example:
Code:
mylua = require 'lunajson'
local demojson = mylua.encode( your_object )
As you can see in the above line of code we are trying to use the encode method here. First we have imported the library, then calling the method on its object. Inside the encode method we can pass the object name or variable that we want to convert to json format.
2. Decode: Decode method is used to convert the json again to normal format. We can call this method on the library imported name.
Example:
Code:
mylua = require 'lunajson'
local demojson = mylua.decode( your_object )
As you can see in the above line of code we are trying to use the decode method here. First we have imported the library, then calling the method on its object. Inside the decode method we can pass the object name or variable that we want to convert to again a normal object in lua.
Example of Lua JSON
Given below is the example of Lua JSON:
This program will not run if you do not have luarocks and luajson installed on your machine with required setup and configuration. It will not run using any online compiler for this, it will throw you error.
Code:
#!/usr/bin/lua5.1
print("Demo to show json in lua using lunajson library !!")
myluna = require 'lunajson'
local objecttoconvert = { ["object one "] = "value one", ["object two"] = "value two", ["object three"] = "value three" }
local result = myluna.encode( objecttoconvert )
print("print result !!")
print( result )
Output:
Conclusion
JSON makes the staring of large data a lot easier, it works in the same way like any other programming language. It can contain any type of object inside it, list of objects and so on. Basically it is used for transfer of large amount of data between server and client.
Recommended Articles
We hope that this EDUCBA information on “Lua JSON” was beneficial to you. You can view EDUCBA’s recommended articles for more information.