Lua Academy · Lesson

Handling Nested Data

Work with complex structures.

Lesson 4 of 413 steps

Handling Nested Data is a free Lua Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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)
end

Deeply 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 crash

Iterating 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
end

Building 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
end

Watch 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")
end

Quick 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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Handling Nested Data” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Handling Nested Data”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

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