Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

let json = chilkat::JsonObject::new();

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

if json.load(&json_str).is_err() {
    println!("{}", json.last_error_text());
    return;
}

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

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

// Emit the formatted JSON:
println!("{}", json.emit().unwrap_or_default());