0Pricing
Lua Academy · Lesson

Handling Nested Data

Work with complex structures.

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)

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