Lua
Lua
JSON Copy Objects
See more JSON Examples
Copy objects from one JSON document to another.Chilkat Lua Downloads
local chilkat = require("chilkat")
local json1 = chilkat.JsonObject.new()
json1:update_string("ID1.cn", "Name")
json1:update_string("ID1.objectGUID", "GUID")
json1:update_string("ID2.cn", "Name")
json1:update_string("ID2.objectGUID", "GUID")
json1.emit_compact = false
print(json1:emit())
-- json1 contains:
-- {
-- "ID1": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- },
-- "ID2": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- }
-- }
local json2 = chilkat.JsonObject.new()
json2:update_string("Name1.ID1.cn", "Name")
json2:update_string("Name1.ID1.objectGUID", "GUID")
json2:update_string("Name1.ID2.cn", "Name")
json2:update_string("Name1.ID2.objectGUID", "GUID")
json2:update_string("Name2.ID3.cn", "Name")
json2:update_string("Name2.ID3.objectGUID", "GUID")
json2.emit_compact = false
print(json2:emit())
-- {
-- "Name1": {
-- "ID1": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- },
-- "ID2": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- }
-- },
-- "Name2": {
-- "ID3": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- }
-- }
-- }
-- Copy Name1, Name2 into json1
local i = 0
local numMembers = json2.size
while i < numMembers do
local jsonObj = json2:object_at(i)
json1:append_object_copy(json2:name_at(i), jsonObj)
i = i + 1
end
-- Now see what json1 contains:
print(json1:emit())
-- {
-- "ID1": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- },
-- "ID2": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- },
-- "Name1": {
-- "ID1": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- },
-- "ID2": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- }
-- },
-- "Name2": {
-- "ID3": {
-- "cn": "Name",
-- "objectGUID": "GUID"
-- }
-- }
-- }