Handling Nested Data
Work with complex structures.
Handling Nested Data is a free Lua Academy lesson on CoddyKit — lesson 4 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.
Nested Data Is Everywhere
Real-world JSON is rarely flat. Objects contain objects, and arrays contain objects, often several levels deep.
In Lua this means tables inside tables. The good news: decode and encode handle nesting automatically.
An Object Inside an Object
When a JSON value is itself an object, it decodes to a nested Lua table. You drill down with chained field access.
local json = require("dkjson")
local d = json.decode('{"user": {"name": "Eve", "age": 30}}')
print(d.user.name)
print(d.user.age)An Array of Objects
A very common shape is a JSON array of objects, like a list of records. It decodes to a sequence of tables.
Loop with ipairs and read fields from each item.
local json = require("dkjson")
local d = json.decode('[{"id":1},{"id":2}]')
for _, item in ipairs(d) do
print(item.id)
endDeeply Nested Access
You can chain as deep as the data goes. Each level is just another table lookup.
But if any intermediate level is missing, the chain errors with 'attempt to index a nil value'.
local json = require("dkjson")
local d = json.decode('{"a":{"b":{"c": 42}}}')
print(d.a.b.c)Safe Nested Access
To avoid crashes on missing levels, check each step or use and short-circuiting.
The and chain stops at the first nil and returns nil instead of erroring.
local json = require("dkjson")
local d = json.decode('{"a":{}}')
local c = d.a and d.a.b and d.a.b.c
print(c) -- nil, no crashIterating Nested Arrays
Arrays can hold objects that hold more arrays. Nest your loops to match the structure.
local json = require("dkjson")
local d = json.decode('[{"tags":["a","b"]}]')
for _, rec in ipairs(d) do
for _, tag in ipairs(rec.tags) do
print(tag)
end
endBuilding Nested Tables
To encode nested JSON, simply nest Lua tables. The encoder walks the whole structure recursively.
local json = require("dkjson")
local t = {
user = { name = "Jo", roles = { "admin", "editor" } }
}
print(json.encode(t, { indent = true }))Modifying Nested Values
You can read, change, and add fields at any depth before re-encoding. This is the heart of transforming data.
local json = require("dkjson")
local d = json.decode('{"cfg":{"volume":5}}')
d.cfg.volume = 8
d.cfg.muted = false
print(json.encode(d))Mixing Arrays and Objects
JSON freely mixes arrays and objects at different levels. Lua mirrors this with sequence tables and keyed tables nested together.
Just make sure each table is consistently one shape or the other.
local json = require("dkjson")
local t = {
team = "red",
members = { { id = 1 }, { id = 2 } }
}
print(json.encode(t))Walking Unknown Structures
When you do not know the shape in advance, use type() to inspect each value and recurse into tables.
This lets you write generic processors for arbitrary JSON.
local function walk(v)
if type(v) == "table" then
for k, sub in pairs(v) do walk(sub) end
else
print(v)
end
endWatch for null in Nesting
A null deep inside nested data still decodes to json.null in dkjson. Account for it when walking structures so you do not mistake it for a real table or value.
local json = require("dkjson")
local d = json.decode('{"a":{"b": null}}')
if d.a.b == json.null then
print("b is null")
endQuick Check
Test your handling of nested JSON in Lua.
Recap
Nested JSON maps to tables inside tables; decode and encode handle the recursion for you.
Drill down with chained access, but guard against missing levels using and chains. Build nested output by nesting Lua tables, and remember nulls and the array-vs-object distinction at every depth.
Frequently asked questions
Is the “Handling Nested Data” lesson free?
Yes — the full text of “Handling Nested Data” 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 “Handling Nested Data”?
Work with complex structures. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Handling Nested Data” 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
- Why JSON
- Decoding JSON to Tables
- Encoding Tables to JSON
- Handling Nested Data