Sample code for 30+ languages & platforms
Zig

Pretty Print JSON (Formatter, Beautifier)

See more JSON Examples

Demonstrates how to emit JSON in a pretty, human-readable format with indenting of nested arrays and objects.

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 json = try chilkat.JsonObject.init();
    defer json.deinit();

    const json_str = "{\"name\": \"donut\",\"image\":{\"fname\": \"donut.jpg\",\"w\": 200,\"h\": 200},\"thumbnail\":{\"fname\": \"donutThumb.jpg\",\"w\": 32,\"h\": 32}}";

    json.load(json_str) catch {
        std.debug.print("{s}\n", .{try json.getLastErrorText(alloc)});
        return;
    };

    // To pretty-print, set the EmitCompact property equal to false
    json.setEmitCompact(false);

    // If bare-LF line endings are desired, turn off EmitCrLf
    // Otherwise CRLF line endings are emitted.
    json.setEmitCrLf(false);

    // Emit the formatted JSON:
    std.debug.print("{s}\n", .{try json.emit(alloc)});
}