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

Copy JSON Object from one JSON Array to Another

See more JSON Examples

Demonstrates how to copy an object in a JSON array to another JSON array.

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 arr1 = try chilkat.JsonArray.init();
    defer arr1.deinit();
    const arr2 = try chilkat.JsonArray.init();
    defer arr2.deinit();

    const s = "[{\"a\":1}, {\"b\":2}, {\"c\":3}]";
    const s_empty = "[]";

    arr1.load(s) catch {};
    arr2.load(s_empty) catch {};

    const j_obj = try chilkat.JsonObject.init();
    defer j_obj.deinit();
    arr1.objectAt2(1, j_obj) catch {};

    arr2.addObjectCopyAt(-1, j_obj) catch {};

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

    // output is:   [{"b":2}]
}