Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

set json [new_CkJsonObject]

set jsonStr "{\"name\": \"donut\",\"image\":{\"fname\": \"donut.jpg\",\"w\": 200,\"h\": 200},\"thumbnail\":{\"fname\": \"donutThumb.jpg\",\"w\": 32,\"h\": 32}}"

set success [CkJsonObject_Load $json $jsonStr]
if {$success != 1} then {
    puts [CkJsonObject_lastErrorText $json]
    delete_CkJsonObject $json
    exit
}

# To pretty-print, set the EmitCompact property equal to 0
CkJsonObject_put_EmitCompact $json 0

# If bare-LF line endings are desired, turn off EmitCrLf
# Otherwise CRLF line endings are emitted.
CkJsonObject_put_EmitCrLf $json 0

# Emit the formatted JSON:
puts [CkJsonObject_emit $json]

delete_CkJsonObject $json