Sample code for 30+ languages & platforms
Zig

Shopify Get particular fields of a single product

See more Shopify Examples

Get particular fields of a single product

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 rest = try chilkat.Rest.init();
    defer rest.deinit();

    rest.setAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_KEY") catch {};

    rest.connect("chilkat.myshopify.com", 443, true, true) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const sb_json = try chilkat.StringBuilder.init();
    defer sb_json.deinit();
    rest.fullRequestNoBodySb("GET", "/admin/products/#{id}.json?fields=id,images,title", sb_json) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("Received error response code: {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("Response body:\n", .{});
        std.debug.print("{s}\n", .{try sb_json.getAsString(alloc)});
        return;
    }

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

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.
    var product_id: i32 = 0;
    var i: i32 = 0;
    var id: i32 = 0;

    var position: i32 = 0;
    var created_at: [:0]const u8 = "";
    var updated_at: [:0]const u8 = "";
    var width: i32 = 0;
    var height: i32 = 0;
    var src: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var int_val: i32 = 0;

    product_id = json.intOf("product.id");
    _ = try json.stringOf(alloc, "product.title");
    i = 0;
    const count_i = json.sizeOfArray("product.images");
    while (i < count_i) {
        json.setI(i);
        id = json.intOf("product.images[i].id");
        product_id = json.intOf("product.images[i].product_id");
        position = json.intOf("product.images[i].position");
        created_at = try json.stringOf(alloc, "product.images[i].created_at");
        updated_at = try json.stringOf(alloc, "product.images[i].updated_at");
        width = json.intOf("product.images[i].width");
        height = json.intOf("product.images[i].height");
        src = try json.stringOf(alloc, "product.images[i].src");
        j = 0;
        count_j = json.sizeOfArray("product.images[i].variant_ids");
        while (j < count_j) {
            json.setJ(j);
            int_val = json.intOf("product.images[i].variant_ids[j]");
            j = j + 1;
        }

        i = i + 1;
    }

    // A sample JSON response body that is parsed by the above code:

    // {
    //   "product": {
    //     "id": 632910392,
    //     "title": "IPod Nano - 8GB",
    //     "images": [
    //       {
    //         "id": 850703190,
    //         "product_id": 632910392,
    //         "position": 1,
    //         "created_at": "2017-09-22T14:08:02-04:00",
    //         "updated_at": "2017-09-22T14:08:02-04:00",
    //         "width": 123,
    //         "height": 456,
    //         "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano.png?v=1506103682",
    //         "variant_ids": [
    //         ]
    //       },
    //       {
    //         "id": 562641783,
    //         "product_id": 632910392,
    //         "position": 2,
    //         "created_at": "2017-09-22T14:08:02-04:00",
    //         "updated_at": "2017-09-22T14:08:02-04:00",
    //         "width": 123,
    //         "height": 456,
    //         "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano-2.png?v=1506103682",
    //         "variant_ids": [
    //           808950810
    //         ]
    //       }
    //     ]
    //   }
    // }

    std.debug.print("Example Completed.\n", .{});
}