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

Insert JSON Array into another JSON Object

See more JSON Examples

Demonstrates how to insert a JSON array into a JSON 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 have two separate JSON objects.
    const json_a = try chilkat.JsonObject.init();
    defer json_a.deinit();
    json_a.updateString("ciphertext", "encryptedData") catch {};
    json_a.updateInt("status", 200) catch {};
    json_a.updateString("error", "errorMsg") catch {};

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

    // jsonA contains:

    // {
    //   "ciphertext": "encryptedData",
    //   "status": 200,
    //   "error": "errorMsg"
    // }

    const json_b = try chilkat.JsonObject.init();
    defer json_b.deinit();
    json_b.updateString("users[0].role", "Surgeon") catch {};
    json_b.updateNewArray("users[0].sub_roles") catch {};
    json_b.updateBool("users[0].viewable_for_sharing", true) catch {};
    json_b.updateInt("users[0].eula_create_date", 123) catch {};
    json_b.updateString("users[1].role", "Support") catch {};
    json_b.updateString("users[1].sub_roles[0]", "Tech") catch {};
    json_b.updateString("users[1].sub_roles[1]", "Service") catch {};
    json_b.updateBool("users[1].viewable_for_sharing", true) catch {};
    json_b.updateInt("users[1].eula_create_date", 123) catch {};

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

    // jsonB contains:

    // {
    //   "users": [
    //     {
    //       "role": "Surgeon",
    //       "sub_roles": [],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 1649108922482
    //     },
    //     {
    //       "role": "Support",
    //       "sub_roles": [
    //         "Tech",
    //         "Service"
    //       ],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 1649108951523
    //     }
    //   ]
    // }

    // Let's say we want to insert jsonB into jsonA to get this:

    // {
    //   "ciphertext": "encryptedData",
    //   "status": 200,
    //   "error": "errorMsg",
    //   "users": [
    //     {
    //       "role": "Surgeon",
    //       "sub_roles": [],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 1649108922482
    //     },
    //     {
    //       "role": "Support",
    //       "sub_roles": [
    //         "Tech",
    //         "Service"
    //       ],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 1649108951523
    //     }
    //   ]
    // }

    // The destination is the empty "users" array, the source is the populated "users" array in jsonB.
    const json_users_dest = try chilkat.JsonArray.init();
    defer json_users_dest.deinit();
    json_a.appendArray2("users", json_users_dest) catch {};

    const json_users_src = try chilkat.JsonArray.init();
    defer json_users_src.deinit();
    json_b.arrayOf2("users", json_users_src) catch {};

    // Copy the array items from source to dest
    json_users_dest.appendArrayItems(json_users_src) catch {};

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

    // The end result is this:

    // {
    //   "ciphertext": "encryptedData",
    //   "status": 200,
    //   "error": "errorMsg",
    //   "users": [
    //     {
    //       "role": "Surgeon",
    //       "sub_roles": [
    //       ],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 123
    //     },
    //     {
    //       "role": "Support",
    //       "sub_roles": [
    //         "Tech",
    //         "Service"
    //       ],
    //       "viewable_for_sharing": true,
    //       "eula_create_date": 123
    //     }
    //   ]
    // }
}