Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.67+

Using Pre-defined JSON Templates

See more JSON Examples

Demonstrates how to predefine a JSON template, and then use it to emit JSON with variable substitutions.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- One way to create JSON is to do it in a straightforward manner:
local json = chilkat.JsonObject.new()
json.emit_compact = false
json:update_string("id", "0001")
json:update_string("type", "donut")
json:update_string("name", "Cake")
json:update_string("image.url", "images/0001.jpg")
json:update_int("image.width", 200)
json:update_int("image.height", 200)
json:update_string("thumbnail.url", "images/thumbnails/0001.jpg")
json:update_int("thumbnail.width", 32)
json:update_int("thumbnail.height", 32)
print(json:emit())

-- The JSON created by the above code:

--     {
--       "id": "0001",
--       "type": "donut",
--       "name": "Cake",
--       "image": {
--         "url": "images/0001.jpg",
--         "width": 200,
--         "height": 200
--       },
--       "thumbnail": {
--         "url": "images/thumbnails/0001.jpg",
--         "width": 32,
--         "height": 32
--       }
--     }

-- An alternative is to predefine a template, and then use it to emit with variable substitutions.
-- For example:

local jsonTemplate = chilkat.JsonObject.new()
jsonTemplate:update_string("id", "{$id}")
jsonTemplate:update_string("type", "donut")
jsonTemplate:update_string("name", "{$name}")
jsonTemplate:update_string("image.url", "{$imageUrl}")
-- The "i." indicates that it's an integer variable.
jsonTemplate:update_string("image.width", "{$i.imageWidth}")
jsonTemplate:update_string("image.height", "{$i.imageHeight}")
jsonTemplate:update_string("thumbnail.url", "{$thumbUrl}")
jsonTemplate:update_string("thumbnail.width", "{$i.thumbWidth}")
jsonTemplate:update_string("thumbnail.height", "{$i.thumbHeight}")
-- Give this template a name.
jsonTemplate:predefine("donut")

-- --------------------------------------------------------------------------
-- OK, the template is defined.  Defining a template can be done once
-- at the start of your program, and you can discard the jsonTemplate object (it
-- doesn't need to stick around..)

-- Now we can create instances of the JSON object by name:
local jsonDonut = chilkat.JsonObject.new()
jsonDonut.emit_compact = false
jsonDonut:load_predefined("donut")
print(jsonDonut:emit())

-- The output is this:

--     {
--       "id": "{$id}",
--       "type": "donut",
--       "name": "{$name}",
--       "image": {
--         "url": "{$imageUrl}",
--         "width": "{$i.imageWidth}",
--         "height": "{$i.imageHeight}"
--       },
--       "thumbnail": {
--         "url": "{$thumbUrl}",
--         "width": "{$i.thumbWidth}",
--         "height": "{$i.thumbHeight}"
--       }
--     }

-- Finally, we can substitute variables like this:
local donutValues = chilkat.Hashtable.new()
donutValues:add_str("id", "0001")
donutValues:add_str("name", "Cake")
donutValues:add_str("imageUrl", "images/0001.jpg")
donutValues:add_int("imageWidth", 200)
donutValues:add_int("imageHeight", 200)
donutValues:add_str("thumbUrl", "images/thumbnails/0001.jpg")
donutValues:add_int("thumbWidth", 32)
donutValues:add_int("thumbHeight", 32)

-- Emit with variable substitutions:
local omitEmpty = true
print(jsonDonut:emit_with_subs(donutValues, omitEmpty))

-- Output:

--     {
--       "id": "0001",
--       "type": "donut",
--       "name": "Cake",
--       "image": {
--         "url": "images/0001.jpg",
--         "width": 200,
--         "height": 200
--       },
--       "thumbnail": {
--         "url": "images/thumbnails/0001.jpg",
--         "width": 32,
--         "height": 32
--       }
--     }

-- Change some of the values:
donutValues:add_str("id", "0002")
donutValues:add_str("imageUrl", "images/0002.jpg")
donutValues:add_str("thumbUrl", "images/thumbnails/0002.jpg")

print(jsonDonut:emit_with_subs(donutValues, omitEmpty))

-- Output:

--     {
--       "id": "0002",
--       "type": "donut",
--       "name": "Cake",
--       "image": {
--         "url": "images/0002.jpg",
--         "width": 200,
--         "height": 200
--       },
--       "thumbnail": {
--         "url": "images/thumbnails/0002.jpg",
--         "width": 32,
--         "height": 32
--       }
--     }