Sample code for 30+ languages & platforms
Zig

Outlook List Contacts

See more Outlook Contact Examples

List Outlook Contacts

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 here: Get Outlook Contacts OAuth2 Access Token (Azure AD v2.0 Endpoint).

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

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

    // Send the following GET:

    // GET https://graph.microsoft.com/v1.0/me/contacts
    const sb_json = try chilkat.StringBuilder.init();
    defer sb_json.deinit();
    http.quickGetSb("https://graph.microsoft.com/v1.0/me/contacts", sb_json) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const status_code = http.getLastStatus();
    std.debug.print("Response status code = {d}\n", .{status_code});

    if (status_code != 200) {
        std.debug.print("{s}\n", .{try sb_json.getAsString(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_json) catch {};
    j_resp.setEmitCompact(false);
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    // Sample output:

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('4ee732c3-322e-4a6b-b729-2fd1eb5c6004')/contacts",
    //   "value": [
    //     {
    //       "@odata.etag": "W/\"EQAAABYAAABUcG0qWqkmQYqWLHQataQxAAD0iYfT\"",
    //       "id": "AAMkADYzZWE3YmZmLWU0YzgtNGNkZC04MGE1LWFiYTFlNTRlY2QwYQBGAAAAAAAu7cUXL5YOTrdsUIw7-v8FBwBUcG0qWqkmQYqWLHQataQxAAAAAAEOAABUcG0qWqkmQYqWLHQataQxAAD0sxexAAA=",
    //       "createdDateTime": "2021-06-29T16:32:05Z",
    //       "lastModifiedDateTime": "2021-06-29T16:32:06Z",
    //       "changeKey": "EQAAABYAAABUcG0qWqkmQYqWLHQataQxAAD0iYfT",
    //       "categories": [
    //       ],
    //       "parentFolderId": "AAMkADYzZWE3YmZmLWU0YzgtNGNkZC04MGE1LWFiYTFlNTRlY2QwYQAuAAAAAAAu7cUXL5YOTrdsUIw7-v8FAQBUcG0qWqkmQYqWLHQataQxAAAAAAEOAAA=",
    //       "birthday": null,
    //       "fileAs": "",
    //       "displayName": "Pavel Bansky",
    //       "givenName": "Pavel",
    //       "initials": null,
    //       "middleName": null,
    //       "nickName": null,
    //       "surname": "Bansky",
    //       "title": null,
    //       "yomiGivenName": null,
    //       "yomiSurname": null,
    //       "yomiCompanyName": null,
    //       "generation": null,
    //       "imAddresses": [
    //       ],
    //       "jobTitle": null,
    //       "companyName": null,
    //       "department": null,
    //       "officeLocation": null,
    //       "profession": null,
    //       "businessHomePage": null,
    //       "assistantName": null,
    //       "manager": null,
    //       "homePhones": [
    //       ],
    //       "mobilePhone": null,
    //       "businessPhones": [
    //         "+1 732 555 0102"
    //       ],
    //       "spouseName": null,
    //       "personalNotes": "",
    //       "children": [
    //       ],
    //       "emailAddresses": [
    //         {
    //           "name": "Pavel Bansky",
    //           "address": "pavelb@fabrikam.onmicrosoft.com"
    //         }
    //       ],
    //       "homeAddress": {},
    //       "businessAddress": {},
    //       "otherAddress": {}
    //     },
    // ...
    //   ]
    // }

    var odata_etag: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var created_date_time: [:0]const u8 = "";
    var last_modified_date_time: [:0]const u8 = "";
    var change_key: [:0]const u8 = "";
    var parent_folder_id: [:0]const u8 = "";
    var birthday: [:0]const u8 = "";
    var file_as: [:0]const u8 = "";
    var display_name: [:0]const u8 = "";
    var given_name: [:0]const u8 = "";
    var initials: [:0]const u8 = "";
    var middle_name: [:0]const u8 = "";
    var nick_name: [:0]const u8 = "";
    var surname: [:0]const u8 = "";
    var title: [:0]const u8 = "";
    var yomi_given_name: [:0]const u8 = "";
    var yomi_surname: [:0]const u8 = "";
    var yomi_company_name: [:0]const u8 = "";
    var generation: [:0]const u8 = "";
    var job_title: [:0]const u8 = "";
    var company_name: [:0]const u8 = "";
    var department: [:0]const u8 = "";
    var office_location: [:0]const u8 = "";
    var profession: [:0]const u8 = "";
    var business_home_page: [:0]const u8 = "";
    var assistant_name: [:0]const u8 = "";
    var manager: [:0]const u8 = "";
    var mobile_phone: [:0]const u8 = "";
    var spouse_name: [:0]const u8 = "";
    var personal_notes: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var str_val: [:0]const u8 = "";
    var name: [:0]const u8 = "";
    var address: [:0]const u8 = "";

    _ = try j_resp.stringOf(alloc, "\"@odata.context\"");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("value");
    while (i < count_i) {
        j_resp.setI(i);
        odata_etag = try j_resp.stringOf(alloc, "value[i].\"@odata.etag\"");
        id = try j_resp.stringOf(alloc, "value[i].id");
        created_date_time = try j_resp.stringOf(alloc, "value[i].createdDateTime");
        last_modified_date_time = try j_resp.stringOf(alloc, "value[i].lastModifiedDateTime");
        change_key = try j_resp.stringOf(alloc, "value[i].changeKey");
        parent_folder_id = try j_resp.stringOf(alloc, "value[i].parentFolderId");
        birthday = try j_resp.stringOf(alloc, "value[i].birthday");
        file_as = try j_resp.stringOf(alloc, "value[i].fileAs");
        display_name = try j_resp.stringOf(alloc, "value[i].displayName");
        given_name = try j_resp.stringOf(alloc, "value[i].givenName");
        initials = try j_resp.stringOf(alloc, "value[i].initials");
        middle_name = try j_resp.stringOf(alloc, "value[i].middleName");
        nick_name = try j_resp.stringOf(alloc, "value[i].nickName");
        surname = try j_resp.stringOf(alloc, "value[i].surname");
        title = try j_resp.stringOf(alloc, "value[i].title");
        yomi_given_name = try j_resp.stringOf(alloc, "value[i].yomiGivenName");
        yomi_surname = try j_resp.stringOf(alloc, "value[i].yomiSurname");
        yomi_company_name = try j_resp.stringOf(alloc, "value[i].yomiCompanyName");
        generation = try j_resp.stringOf(alloc, "value[i].generation");
        job_title = try j_resp.stringOf(alloc, "value[i].jobTitle");
        company_name = try j_resp.stringOf(alloc, "value[i].companyName");
        department = try j_resp.stringOf(alloc, "value[i].department");
        office_location = try j_resp.stringOf(alloc, "value[i].officeLocation");
        profession = try j_resp.stringOf(alloc, "value[i].profession");
        business_home_page = try j_resp.stringOf(alloc, "value[i].businessHomePage");
        assistant_name = try j_resp.stringOf(alloc, "value[i].assistantName");
        manager = try j_resp.stringOf(alloc, "value[i].manager");
        mobile_phone = try j_resp.stringOf(alloc, "value[i].mobilePhone");
        spouse_name = try j_resp.stringOf(alloc, "value[i].spouseName");
        personal_notes = try j_resp.stringOf(alloc, "value[i].personalNotes");
        j = 0;
        count_j = j_resp.sizeOfArray("value[i].categories");
        while (j < count_j) {
            j_resp.setJ(j);
            j = j + 1;
        }

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

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

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

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

        j = 0;
        count_j = j_resp.sizeOfArray("value[i].emailAddresses");
        while (j < count_j) {
            j_resp.setJ(j);
            name = try j_resp.stringOf(alloc, "value[i].emailAddresses[j].name");
            address = try j_resp.stringOf(alloc, "value[i].emailAddresses[j].address");
            j = j + 1;
        }

        i = i + 1;
    }
}