Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

JSON AppendObject2 Example

Demonstrates the AppendObject2 function.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load("{ \"name\": \"John\", \"marbles\": 100 }") catch {};

    // Append an empty object named "addr"
    const j_obj = try chilkat.JsonObject.init();
    defer j_obj.deinit();
    json.appendObject2("addr", j_obj) catch {};

    std.debug.print("{s}\n", .{try json.emit(alloc)});
    // Expected output is:   {"name":"John","marbles":100,"addr":{}}

    // Add members to the object.
    j_obj.appendString("street", "1200 Elm St.") catch {};
    j_obj.appendString("city", "Springfield") catch {};
    j_obj.appendInt("zip", 60606) catch {};

    std.debug.print("{s}\n", .{try json.emit(alloc)});
    // Expected output is:  {"name":"John","marbles":100,"addr":{"street":"1200 Elm St.","city":"Springfield","zip":60606}}
}