Sample code for 30+ languages & platforms
Zig

List Groups

See more Microsoft Group Examples

List all the groups available in an organization, including but not limited to Office 365 Groups.

See https://docs.microsoft.com/en-us/graph/api/group-list?view=graph-rest-1.0 for more information.

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

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Use your previously obtained access token as shown here:
    //    Get Microsoft Graph OAuth2 Access Token with Group.ReadWrite.All scope.

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/msGraphGroup.json") catch {
        std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
        return;
    };

    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    // Send a GET request to https://graph.microsoft.com/v1.0/groups?$orderby=displayName
    const str_response = http.quickGetStr(alloc, "https://graph.microsoft.com/v1.0/groups?$orderby=displayName") catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(str_response) catch {};
    json.setEmitCompact(false);

    if (http.getLastStatus() != 200) {
        std.debug.print("{s}\n", .{try json.emit(alloc)});
        std.debug.print("Failed, response status code = {d}\n", .{http.getLastStatus()});
        return;
    }

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

    // Sample output:
    // (See parsing code below..)

    // {
    //     "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#groups",
    //     "value": [
    //         {
    //             "id": "45b7d2e7-b882-4a80-ba97-10b7a63b8fa4",
    //             "deletedDateTime": null,
    //             "classification": null,
    //             "createdDateTime": "2018-12-22T02:21:05Z",
    //             "creationOptions": [],
    //             "description": "Self help community for golf",
    //             "displayName": "Golf Assist",
    //             "groupTypes": [
    //                 "Unified"
    //             ],
    //             "mail": "golfassist@contoso.com",
    //             "mailEnabled": true,
    //             "mailNickname": "golfassist",
    //             "onPremisesLastSyncDateTime": null,
    //             "onPremisesSecurityIdentifier": null,
    //             "onPremisesSyncEnabled": null,
    //             "preferredDataLocation": "CAN",
    //             "proxyAddresses": [
    //                 "smtp:golfassist@contoso.onmicrosoft.com",
    //                 "SMTP:golfassist@contoso.com"
    //             ],
    //             "renewedDateTime": "2018-12-22T02:21:05Z",
    //             "resourceBehaviorOptions": [],
    //             "resourceProvisioningOptions": [],
    //             "securityEnabled": false,
    //             "visibility": "Public",
    //             "onPremisesProvisioningErrors": []
    //         },
    //         {
    //             "id": "d7797254-3084-44d0-99c9-a3b5ab149538",
    //             "deletedDateTime": null,
    //             "classification": null,
    //             "createdDateTime": "2018-11-19T20:29:40Z",
    //             "creationOptions": [],
    //             "description": "Talk about golf",
    //             "displayName": "Golf Discussion",
    //             "groupTypes": [],
    //             "mail": "golftalk@contoso.com",
    //             "mailEnabled": true,
    //             "mailNickname": "golftalk",
    //             "onPremisesLastSyncDateTime": null,
    //             "onPremisesSecurityIdentifier": null,
    //             "onPremisesSyncEnabled": null,
    //             "preferredDataLocation": "CAN",
    //             "proxyAddresses": [
    //                 "smtp:golftalk@contoso.onmicrosoft.com",
    //                 "SMTP:golftalk@contoso.com"
    //             ],
    //             "renewedDateTime": "2018-11-19T20:29:40Z",
    //             "resourceBehaviorOptions": [],
    //             "resourceProvisioningOptions": [],
    //             "securityEnabled": false,
    //             "visibility": null,
    //             "onPremisesProvisioningErrors": []
    //         }
    //     ]
    // }
    //
    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var i: i32 = 0;
    var id: [:0]const u8 = "";
    var deleted_date_time: [:0]const u8 = "";
    var classification: [:0]const u8 = "";
    var created_date_time: [:0]const u8 = "";
    var description: [:0]const u8 = "";
    var display_name: [:0]const u8 = "";
    var mail: [:0]const u8 = "";
    var mail_enabled: bool = false;
    var mail_nickname: [:0]const u8 = "";
    var on_premises_last_sync_date_time: [:0]const u8 = "";
    var on_premises_security_identifier: [:0]const u8 = "";
    var on_premises_sync_enabled: [:0]const u8 = "";
    var preferred_data_location: [:0]const u8 = "";
    var renewed_date_time: [:0]const u8 = "";
    var security_enabled: bool = false;
    var visibility: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var str_val: [:0]const u8 = "";

    _ = try json.stringOf(alloc, "\"@odata.context\"");
    i = 0;
    const count_i = json.sizeOfArray("value");
    while (i < count_i) {
        json.setI(i);
        id = try json.stringOf(alloc, "value[i].id");
        deleted_date_time = try json.stringOf(alloc, "value[i].deletedDateTime");
        classification = try json.stringOf(alloc, "value[i].classification");
        created_date_time = try json.stringOf(alloc, "value[i].createdDateTime");
        description = try json.stringOf(alloc, "value[i].description");
        display_name = try json.stringOf(alloc, "value[i].displayName");
        mail = try json.stringOf(alloc, "value[i].mail");
        mail_enabled = json.boolOf("value[i].mailEnabled");
        mail_nickname = try json.stringOf(alloc, "value[i].mailNickname");
        on_premises_last_sync_date_time = try json.stringOf(alloc, "value[i].onPremisesLastSyncDateTime");
        on_premises_security_identifier = try json.stringOf(alloc, "value[i].onPremisesSecurityIdentifier");
        on_premises_sync_enabled = try json.stringOf(alloc, "value[i].onPremisesSyncEnabled");
        preferred_data_location = try json.stringOf(alloc, "value[i].preferredDataLocation");
        renewed_date_time = try json.stringOf(alloc, "value[i].renewedDateTime");
        security_enabled = json.boolOf("value[i].securityEnabled");
        visibility = try json.stringOf(alloc, "value[i].visibility");
        j = 0;
        count_j = json.sizeOfArray("value[i].creationOptions");
        while (j < count_j) {
            json.setJ(j);
            // ...
            j = j + 1;
        }

        j = 0;
        count_j = json.sizeOfArray("value[i].groupTypes");
        while (j < count_j) {
            json.setJ(j);
            str_val = try json.stringOf(alloc, "value[i].groupTypes[j]");
            j = j + 1;
        }

        j = 0;
        count_j = json.sizeOfArray("value[i].proxyAddresses");
        while (j < count_j) {
            json.setJ(j);
            str_val = try json.stringOf(alloc, "value[i].proxyAddresses[j]");
            j = j + 1;
        }

        j = 0;
        count_j = json.sizeOfArray("value[i].resourceBehaviorOptions");
        while (j < count_j) {
            json.setJ(j);
            // ...
            j = j + 1;
        }

        j = 0;
        count_j = json.sizeOfArray("value[i].resourceProvisioningOptions");
        while (j < count_j) {
            json.setJ(j);
            // ...
            j = j + 1;
        }

        j = 0;
        count_j = json.sizeOfArray("value[i].onPremisesProvisioningErrors");
        while (j < count_j) {
            json.setJ(j);
            // ...
            j = j + 1;
        }

        i = i + 1;
    }

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