Zig Requires Chilkat v11.0.0+
Zig
Modify Parts of JSON Document
See more JSON Examples
Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:
{
"fruit": [
{
"kind": "apple",
"count": 24,
"fresh": true,
"extraInfo": null,
"listA": [ "abc", 1, null, false ],
"objectB": { "animal" : "monkey" }
},
{
"kind": "pear",
"count": 18,
"fresh": false,
"extraInfo": null
"listA": [ "xyz", 24, null, true ],
"objectB": { "animal" : "lemur" }
}
],
"list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
"alien" : true
}
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();
// Load the JSON from a file.
json.loadFile("qa_data/json/modifySample.json") catch {
std.debug.print("{s}\n", .{try json.getLastErrorText(alloc)});
return;
};
// This example will not check for errors (i.e. null / false / 0 return values)...
// Get the "list" array:
const list_a = try chilkat.JsonArray.init();
defer list_a.deinit();
json.arrayOf2("list", list_a) catch {};
// Modify values in the list.
// Change banana to plantain
try list_a.setStringAt(0, "plantain");
// Change 12 to 24
try list_a.setIntAt(1, 24);
// Change true to false
try list_a.setBoolAt(2, false);
// Is the 3rd item null?
_ = list_a.isNullAt(3);
// Change "orange" to 32.
try list_a.setIntAt(4, 32);
// Change 12.5 to 31.2
try list_a.setNumberAt(5, "31.2");
// Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
// Do this by deleting, then inserting a new object at the same location.
try list_a.deleteAt(6);
const ticker_obj = try chilkat.JsonObject.init();
defer ticker_obj.deinit();
list_a.addObjectAt2(6, ticker_obj) catch {};
try ticker_obj.appendString("ticker", "GOOG");
// Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
try list_a.deleteAt(7);
const aa = try chilkat.JsonArray.init();
defer aa.deinit();
list_a.addArrayAt2(7, aa) catch {};
try aa.addStringAt(-1, "apple");
try aa.addIntAt(-1, 22);
try aa.addBoolAt(-1, true);
try aa.addNullAt(-1);
try aa.addNumberAt(-1, "1080.25");
// Get the "fruit" array
const a_fruit = try chilkat.JsonArray.init();
defer a_fruit.deinit();
json.arrayAt2(0, a_fruit) catch {};
// Get the 1st element:
const apple_obj = try chilkat.JsonObject.init();
defer apple_obj.deinit();
a_fruit.objectAt2(0, apple_obj) catch {};
// Modify values by member name:
try apple_obj.setStringOf("fruit", "fuji_apple");
try apple_obj.setIntOf("count", 46);
try apple_obj.setBoolOf("fresh", false);
try apple_obj.setStringOf("extraInfo", "developed by growers at the Tohoku Research Station in Fujisaki");
// Modify values by index:
const pear_obj = try chilkat.JsonObject.init();
defer pear_obj.deinit();
a_fruit.objectAt2(1, pear_obj) catch {};
try pear_obj.setStringAt(0, "bartlett_pear");
try pear_obj.setIntAt(1, 12);
try pear_obj.setBoolAt(2, false);
try pear_obj.setStringAt(3, "harvested in late August to early September");
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
}