Zig
Zig
JSON: Renaming and Deleting Members
See more JSON Examples
Demonstrates renaming and deleting members. This example uses the following JSON document:
{
"apple": "red",
"lime": "green",
"banana": "yellow",
"broccoli": "green",
"strawberry": "red"
}
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load("{\"apple\": \"red\",\"lime\": \"green\",\"banana\": \"yellow\",\"broccoli\": \"green\",\"strawberry\": \"red\"}") catch {
std.debug.print("{s}\n", .{try json.getLastErrorText(alloc)});
return;
};
// Rename "lime" to "lemon".
try json.rename("lime", "lemon");
// Change the color to yellow:
try json.setStringOf("lemon", "yellow");
// Rename by index. Banana is at index 2 (apple is at index 0)
try json.renameAt(2, "bartlett_pear");
// Delete broccoli by name
try json.delete("broccoli");
// Delete apple by index. Apple is at index 0.
try json.deleteAt(0);
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
}