0Pricing
Lua Academy · Lesson

Why JSON

Data interchange basics.

Why JSON is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight text format for storing and exchanging data. It is human-readable and language-independent.

Almost every web API, config file, and database speaks JSON. Learning to convert between JSON and Lua tables lets your scripts talk to the outside world.

JSON Looks Like Lua

JSON objects use curly braces with key-value pairs, and arrays use square brackets. This maps almost perfectly onto Lua tables.

A JSON object becomes a Lua table with string keys; a JSON array becomes a Lua table with numeric indices starting at 1.

-- JSON text
-- {"name": "Ada", "age": 36}

-- Equivalent Lua table
local person = { name = "Ada", age = 36 }
print(person.name)

Lua Has No Built-in JSON

Unlike some languages, standard Lua does not ship with JSON support. You must use a library.

The two most common choices are dkjson (pure Lua, easy to install) and lua-cjson (a fast C library). Both expose encode and decode functions.

Loading a JSON Library

You load a JSON library with require, just like any other Lua module. The returned table holds the functions you need.

Here we load dkjson and bind it to a local variable named json.

local json = require("dkjson")
-- now json.encode and json.decode are ready

The Two Core Operations

There are only two things you ever do with JSON: decode and encode.

Decoding turns a JSON string into a Lua table you can read. Encoding turns a Lua table back into a JSON string you can send or save.

local json = require("dkjson")
local t = json.decode('{"ok": true}')  -- string -> table
local s = json.encode(t)                -- table -> string

A Decode Example

When you receive JSON text from a file or an API, json.decode parses it into a usable Lua table.

After decoding, you access fields with normal Lua syntax.

local json = require("dkjson")
local text = '{"city": "Oslo", "temp": 7}'
local data = json.decode(text)
print(data.city, data.temp)

An Encode Example

To build JSON, fill a Lua table and pass it to json.encode. The result is a string ready to write or transmit.

local json = require("dkjson")
local book = { title = "Lua", pages = 328 }
local text = json.encode(book)
print(text)

JSON Data Types

JSON supports a small set of types: strings, numbers, booleans, null, objects, and arrays.

Lua maps these to strings, numbers, booleans, nil, and tables. Note that JSON null has no perfect Lua equivalent, which can cause surprises.

Why JSON Over Custom Formats

You could invent your own text format, but JSON is already understood everywhere. Editors highlight it, validators check it, and other languages parse it instantly.

Using JSON makes your data portable and your code easier to debug.

Common Use Cases

You will reach for JSON when calling REST APIs, reading config files, caching results, or passing data between processes.

In each case the pattern is the same: decode incoming JSON to a table, work with it, then encode it back when you need to send it on.

local json = require("dkjson")
-- typical round trip
local data = json.decode(incoming)
data.seen = true
local out = json.encode(data)

Handle Decode Errors

Decoding can fail if the text is not valid JSON. dkjson returns the value, a position, and an error message instead of crashing.

Always check whether the first return value is nil before using it.

local json = require("dkjson")
local data, pos, err = json.decode("{bad")
if not data then
  print("Invalid JSON:", err)
end

Quick Check

Test your understanding of JSON basics in Lua.

Recap

JSON is a universal data format that maps cleanly onto Lua tables. Standard Lua lacks JSON support, so you load a library like dkjson or cjson.

The two core operations are json.decode (string to table) and json.encode (table to string). Next you will decode JSON into tables in detail.

Frequently asked questions

Is the “Why JSON” lesson free?

Yes — the full text of “Why JSON” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Why JSON”?

Data interchange basics. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why JSON” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why JSON
  2. Decoding JSON to Tables
  3. Encoding Tables to JSON
  4. Handling Nested Data
← Back to Lua Academy