Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.1.0+

Create a JSON Array of Objects

See more JSON Examples

Demonstrates how to create a JSON array of objects.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

local arr = chilkat.JsonArray.new()

local obj = chilkat.JsonObject.new()

-- Create a new and empty JSON object in the 1st position of the JSON array
-- and return the reference in the last argument.
arr:add_object_at2(0, obj)
obj:update_string("Name", "Otto")
obj:update_int("Age", 29)
obj:update_bool("Married", false)

-- Create a new and empty JSON object in the 2nd position of the JSON array
-- and return the reference in the last argument.
arr:add_object_at2(1, obj)
obj:update_string("Name", "Connor")
obj:update_int("Age", 43)
obj:update_bool("Married", true)

-- Create a new and empty JSON object in the 3rd position of the JSON array
-- and return the reference in the last argument.
arr:add_object_at2(2, obj)
obj:update_string("Name", "Ramona")
obj:update_int("Age", 34)
obj:update_bool("Married", true)

-- Examine what we have:
arr.emit_compact = false
print(arr:emit())

-- The output is:

-- [
--   {
--     "Name": "Otto",
--     "Age": 29,
--     "Married": false
--   },
--   {
--     "Name": "Connor",
--     "Age": 43,
--     "Married": true
--   },
--   {
--     "Name": "Ramona",
--     "Age": 34,
--     "Married": true
--   }
-- ]