Zig
Zig
JSON Copy Objects
See more JSON Examples
Copy objects from one JSON document to another.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const json1 = try chilkat.JsonObject.init();
defer json1.deinit();
json1.updateString("ID1.cn", "Name") catch {};
json1.updateString("ID1.objectGUID", "GUID") catch {};
json1.updateString("ID2.cn", "Name") catch {};
json1.updateString("ID2.objectGUID", "GUID") catch {};
json1.setEmitCompact(false);
std.debug.print("{s}\n", .{try json1.emit(alloc)});
// json1 contains:
// {
// "ID1": {
// "cn": "Name",
// "objectGUID": "GUID"
// },
// "ID2": {
// "cn": "Name",
// "objectGUID": "GUID"
// }
// }
const json2 = try chilkat.JsonObject.init();
defer json2.deinit();
json2.updateString("Name1.ID1.cn", "Name") catch {};
json2.updateString("Name1.ID1.objectGUID", "GUID") catch {};
json2.updateString("Name1.ID2.cn", "Name") catch {};
json2.updateString("Name1.ID2.objectGUID", "GUID") catch {};
json2.updateString("Name2.ID3.cn", "Name") catch {};
json2.updateString("Name2.ID3.objectGUID", "GUID") catch {};
json2.setEmitCompact(false);
std.debug.print("{s}\n", .{try json2.emit(alloc)});
// {
// "Name1": {
// "ID1": {
// "cn": "Name",
// "objectGUID": "GUID"
// },
// "ID2": {
// "cn": "Name",
// "objectGUID": "GUID"
// }
// },
// "Name2": {
// "ID3": {
// "cn": "Name",
// "objectGUID": "GUID"
// }
// }
// }
// Copy Name1, Name2 into json1
var i: i32 = 0;
const num_members = json2.getSize();
while (i < num_members) {
const json_obj = try json2.objectAt(i);
defer json_obj.deinit();
json1.appendObjectCopy(try json2.nameAt(alloc, i), json_obj) catch {};
i = i + 1;
}
// Now see what json1 contains:
std.debug.print("{s}\n", .{try json1.emit(alloc)});
// {
// "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"
// }
// }
// }
}