Zig Requires Chilkat v11.0.0+
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Imagine we have two separate JSON objects.
const json_a = try chilkat.JsonObject.init();
defer json_a.deinit();
json_a.updateString("animal", "zebra") catch {};
json_a.updateString("colors[0]", "white") catch {};
json_a.updateString("colors[1]", "black") catch {};
json_a.setEmitCompact(false);
std.debug.print("{s}\n", .{try json_a.emit(alloc)});
// jsonA contains:
// {
// "animal": "zebra",
// "colors": [
// "white",
// "black"
// ]
// }
const json_b = try chilkat.JsonObject.init();
defer json_b.deinit();
json_b.updateString("type", "mammal") catch {};
json_b.updateBool("carnivore", false) catch {};
json_b.setEmitCompact(false);
std.debug.print("{s}\n", .{try json_b.emit(alloc)});
// 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"
// ]
// }
json_a.addObjectCopyAt(1, "info", json_b) catch {};
std.debug.print("{s}\n", .{try json_a.emit(alloc)});
// The result is this:
// {
// "animal": "zebra",
// "info": {
// "type": "mammal",
// "carnivore": false
// },
// "colors": [
// "white",
// "black"
// ]
// }
}