Sample code for 30+ languages & platforms
Zig

Firebase JSON Put and Patch

See more JSON Examples

Demonstrates how to apply Firebase put and patch events to a JSON database.

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 json1 = "{\"a\": 1, \"b\": 2}";

    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    // Use Firebase delimiters for JSON paths.
    json.setDelimiterChar("/");

    json.load(json1) catch {};
    json.firebasePut("/c", "{\"foo\": true, \"bar\": false}") catch {};
    // Output should be: {"a":1,"b":2,"c":{"foo":true,"bar":false}}
    std.debug.print("1) {s}\n", .{try json.emit(alloc)});

    json.firebasePut("/c", "\"hello world\"") catch {};
    // Output should be: {"a":1,"b":2,"c":"hello world"}
    std.debug.print("2) {s}\n", .{try json.emit(alloc)});

    json.firebasePut("/c", "{\"foo\": \"abc\", \"bar\": 123}") catch {};
    // Output should be: {"a":1,"b":2,"c":{"foo":"abc","bar":123}}
    std.debug.print("3) {s}\n", .{try json.emit(alloc)});

    // Back to the original..
    json.firebasePut("/", "{\"a\": 1, \"b\": 2}") catch {};
    std.debug.print("4) {s}\n", .{try json.emit(alloc)});

    json.firebasePut("/c", "{\"foo\": true, \"bar\": false}") catch {};
    json.firebasePatch("/c", "{\"foo\": 3, \"baz\": 4}") catch {};
    // Output should be: {"a":1,"b":2,"c":{"foo":3,"bar":false,"baz":4}}
    std.debug.print("5) {s}\n", .{try json.emit(alloc)});

    json.firebasePatch("/c", "{\"foo\": \"abc123\", \"baz\": {\"foo\": true, \"bar\": false}, \"bax\": {\"foo\": 200, \"bar\": 400} }") catch {};
    // Output should be: {"a":1,"b":2,"c":{"foo":"abc123","bar":false,"baz":{"foo":true,"bar":false},"bax":{"foo":200,"bar":400}}}
    std.debug.print("6) {s}\n", .{try json.emit(alloc)});
}