Sample code for 30+ languages & platforms
Zig

Load a JSON Array

See more JSON Examples

The Chilkat JSON API requires the top-level JSON to be an object. Therefore, to load an array requires that it first be wrapped as an object.

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();

    // Imagine we want to load this JSON array for parsing:
    const json_array_str = "[{\"id\":200},{\"id\":196}]";

    // First wrap it in a JSON object by prepending "{ "array":" and appending "}"
    const sb_json = try chilkat.StringBuilder.init();
    defer sb_json.deinit();
    sb_json.append("{\"array\":") catch {};
    sb_json.append(json_array_str) catch {};
    sb_json.append("}") catch {};

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(try sb_json.getAsString(alloc)) catch {};

    // Now we can get the JSON array
    const j_array = try json.arrayAt(0);
    defer j_array.deinit();

    // Do what you want with the JSON array...
    // For example:
    const j_obj_id = try j_array.objectAt(0);
    defer j_obj_id.deinit();
    std.debug.print("{d}\n", .{j_obj_id.intOf("id")});
}