Lua Requires Chilkat v11.0.0+
Lua
Insert JSON Object into another JSON Object
See more JSON Examples
Demonstrates how to insert one JSON object into another. Effectively, the JSON object must be copied into the other..Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Imagine we have two separate JSON objects.
local jsonA = chilkat.JsonObject.new()
jsonA:update_string("animal", "zebra")
jsonA:update_string("colors[0]", "white")
jsonA:update_string("colors[1]", "black")
jsonA.emit_compact = false
print(jsonA:emit())
-- jsonA contains:
-- {
-- "animal": "zebra",
-- "colors": [
-- "white",
-- "black"
-- ]
-- }
local jsonB = chilkat.JsonObject.new()
jsonB:update_string("type", "mammal")
jsonB:update_bool("carnivore", false)
jsonB.emit_compact = false
print(jsonB:emit())
-- jsonB contains:
-- {
-- "type": "mammal",
-- "carnivore": false
-- }
-- Let's say we want to insert jsonB into jsonA to get this:
-- {
-- "animal": "zebra",
-- "info" " {
-- "type": "mammal",
-- "carnivore": false
-- },
-- "colors": [
-- "white",
-- "black"
-- ]
-- }
jsonA:add_object_copy_at(1, "info", jsonB)
print(jsonA:emit())
-- The result is this:
-- {
-- "animal": "zebra",
-- "info": {
-- "type": "mammal",
-- "carnivore": false
-- },
-- "colors": [
-- "white",
-- "black"
-- ]
-- }