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
- Why JSON
- Decoding JSON to Tables
- Encoding Tables to JSON
- Handling Nested Data