Sample code for 30+ languages & platforms
Zig

Using JSON StringOf with Non-String Members

See more JSON Examples

If a JSON member is a boolean, integer, or null, using StringOf will return its string representation.

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

    // Create JSON with members of different data types.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    json.updateInt("a", 123) catch {};
    json.updateBool("b", true) catch {};
    json.updateNull("c") catch {};

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

    // Resulting JSON:
    // {
    //   "a": 123,
    //   "b": true,
    //   "c": null
    // }

    const a = try json.stringOf(alloc, "a");
    std.debug.print("{s}\n", .{a});

    const b = try json.stringOf(alloc, "b");
    std.debug.print("{s}\n", .{b});

    const c = try json.stringOf(alloc, "c");
    std.debug.print("{s}\n", .{c});

    // Output

    // 123
    // true
    // null

    // If you want to get the integer, boolean, or null value
    // you need to use the methods matching the data type
    _ = json.intOf("a");
    _ = json.boolOf("b");
    _ = json.isNullOf("c");
}