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

Example: JsonObject.NewObjectOf method

Demonstrates the NewObjectOf method.

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 car = "{\"make\":\"Toyota\",\"model\":\"Camry\",\"year\":2022,\"color\":\"silver\",\"engine\":{\"type\":\"inline-4\",\"fuel\":\"gasoline\",\"horsepower\":203},\"features\":[\"bluetooth\",\"backup camera\",\"adaptive cruise control\",\"lane assist\"],\"isElectric\":false}";

    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    json.updateString("test", "abc") catch {};

    json.newObjectOf("car", car) catch {
        std.debug.print("{s}\n", .{try json.getLastErrorText(alloc)});
        return;
    };

    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Result:

    // {
    //   "test": "abc",
    //   "car": {
    //     "make": "Toyota",
    //     "model": "Camry",
    //     "year": 2022,
    //     "color": "silver",
    //     "engine": {
    //       "type": "inline-4",
    //       "fuel": "gasoline",
    //       "horsepower": 203
    //     },
    //     "features": [
    //       "bluetooth",
    //       "backup camera",
    //       "adaptive cruise control",
    //       "lane assist"
    //     ],
    //     "isElectric": false
    //   }
    // }
}