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

JSON AppendArray2 Example

Demonstrates the AppendArray2 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 array named "xyz"
    const jarr = try chilkat.JsonArray.init();
    defer jarr.deinit();
    json.appendArray2("xyz", jarr) catch {};

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

    // Add elements to the array.
    jarr.addStringAt(-1, "hello") catch {};
    jarr.addIntAt(-1, 256) catch {};

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