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

Create a JSON Array Containing an Object

See more JSON Examples

Creates a top-level JSON array containing 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();

    const j_array = try chilkat.JsonArray.init();
    defer j_array.deinit();

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    j_array.addObjectAt2(0, json) catch {};

    json.updateString("groupId", "") catch {};
    json.updateString("sku", "") catch {};
    json.updateString("title", "") catch {};
    json.updateString("barcode", "") catch {};
    json.updateString("category", "") catch {};
    json.updateString("description", "") catch {};
    json.updateString("images[0]", "url1") catch {};
    json.updateString("images[1]", "url...") catch {};
    json.updateString("isbn", "") catch {};
    json.updateString("link", "") catch {};
    json.updateString("linkLomadee", "") catch {};
    json.updateString("prices[0].type", "") catch {};
    json.updateNumber("prices[0].price", "0") catch {};
    json.updateNumber("prices[0].priceLomadee", "0") catch {};
    json.updateNumber("prices[0].priceCpa", "0") catch {};
    json.updateNumber("prices[0].installment", "0") catch {};
    json.updateNumber("prices[0].installmentValue", "0") catch {};
    json.updateString("productAttributes.\"Atributo 1\"", "Valor 1") catch {};
    json.updateString("productAttributes.\"Atributo ...\"", "Valor ...") catch {};
    json.updateString("technicalSpecification.\"Especificação 1\"", "Valor") catch {};
    json.updateString("technicalSpecification.\"Especificação ...\"", "Valor ...") catch {};
    json.updateNumber("quantity", "0") catch {};
    json.updateNumber("sizeHeight", "0") catch {};
    json.updateNumber("sizeLength", "0") catch {};
    json.updateNumber("sizeWidth", "0") catch {};
    json.updateNumber("weightValue", "0") catch {};
    json.updateNumber("declaredPrice", "0") catch {};
    json.updateNumber("handlingTimeDays", "0") catch {};
    json.updateBool("marketplace", false) catch {};
    json.updateString("marketplaceName", "") catch {};

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

    // The output of this program is:

    // [
    //   {
    //     "groupId": "",
    //     "sku": "",
    //     "title": "",
    //     "barcode": "",
    //     "category": "",
    //     "description": "",
    //     "images": [
    //       "url1",
    //       "url..."
    //     ],
    //     "isbn": "",
    //     "link": "",
    //     "linkLomadee": "",
    //     "prices": [
    //       {
    //         "type": "",
    //         "price": 0,
    //         "priceLomadee": 0,
    //         "priceCpa": 0,
    //         "installment": 0,
    //         "installmentValue": 0
    //       }
    //     ],
    //     "productAttributes": {
    //       "Atributo 1": "Valor 1",
    //       "Atributo ...": "Valor ..."
    //     },
    //     "technicalSpecification": {
    //       "Especificação 1": "Valor",
    //       "Especificação ...": "Valor ..."
    //     },
    //     "quantity": 0,
    //     "sizeHeight": 0,
    //     "sizeLength": 0,
    //     "sizeWidth": 0,
    //     "weightValue": 0,
    //     "declaredPrice": 0,
    //     "handlingTimeDays": 0,
    //     "marketplace": false,
    //     "marketplaceName": ""
    //   }
    // ]
}