- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Encode and Decode JSON and Lua Programming?
JSON is short for JavaScript Object Notation. It is a type of format that is very widely used in the programming world, but it is only a text format in its entirety. There are many JSON libraries available for Lua, but the most commonly used library is lunajson.
In this article, we will learn how to install lunajson first with the help of luarocks and then we will see how to work with luna-json and use it to cover the most common cases of encoding and decoding a string to JSON or vice versa. Finally, we’ll go over some more applicable use cases for JSON.
Installing Lunajson
In order to install Lunajson, we need to install luarocks first, and then with one simple line we can install lunajson, to install luarocks, we need to follow the commands shown below −
sudo apt install luarocks
On a Mac device, we can write the below commands to install Luarocks.
brew update brew install luarocks
For Windows, we need to follow the guide present at this link.
Now, we just need to install Lunajson. For that, just type the following command in your terminal −
sudo luarocks install lunajson
The above command works on both Mac and Linux, and for windows we need to type the following command −
luarocks install lunajson
Decoding JSON
Now that we know how to set up lunajson on our local machines, it's time to make use of the library to decode JSON.
Consider the example shown below −
Example
#!/usr/bin/lua5.1 lunajson = require 'lunajson' local jsonraw = '{"test":[1,2,3]}' local jsonparse = lunajson.decode( jsonraw ) print( jsonparse["test"][ 1 ] .. ", " .. jsonparse["test"][ 2 ] .. ", " .. jsonparse["test"][ 3 ] .. ")
Output
./json1.lua 1, 2, 3
Encoding JSON
Consider the example shown below that depicts the use case of encoding the JSON.
Example
#!/usr/bin/lua5.1 luna = require 'lunajson' local test = { ["cat"] = { ["name"] = "MeowPow", ["age"] = 5 }, ["dog"] = { ["name"] = "Good Boyyyy", ["age"] = 12 } } local json = luna.encode( test ) print( json )
Output
./json3.lua {"cat":{"name":"MeowPow","age":5},"dog":{"name":"Good Boyyyy","age":12}}
- Related Articles
- How to encode and decode URl in typescript?
- Arduino – base64 encode and decode
- How to encode and decode a URL in JavaScript?
- Encode and Decode Strings in C++
- Encode and decode uuencode files using Python
- Encode and decode binhex4 files using Python (binhex)
- Encode and decode XDR data using Python xdrlib
- Encode and decode MIME quoted-printable data using Python
- How to define and call a function in Lua Programming?
- Difference between . and : in Lua programming
- Differences between Javascript and Lua programming
- How to Use lua-mongo library in Lua Programming?
- How to decode JSON into objects in Golang?
- How to decode JSON objects in Laravel and apply for each loop on that?
- How to convert JSON string into Lua table?
