util::json::indent (public)

 util::json::indent [ -simplify ] json

Defined in packages/acs-tcl/tcl/json-procs.tcl

Indent a JSON string to make it more easily digestable by the human mind. This works best (by far) if the JSON string doesn't already contain newlines (as will be true of JSON strings generated by util::json::gen).

Switches:
-simplify
(boolean) (optional)
If true, remove all fields that don't contribute to the structure of the object/array combination being described by the string.
Parameters:
json - The string to indent
Returns:
The beautifully indented, and optionally simplified, string

Partial Call Graph (max 5 caller/called nodes):
%3

Testcases:
No testcase defined.
Source code:
    set indent -1
    set output ""
    set json [string map {, ,\n :\{ :\n\{ :\[ :\[\n} $json]
    foreach jsonette [split $json \n] {
        if { $simplify_p && ![regexp {[\{\[\}\]]} $jsonette] } {
            continue
        }
        set incr_indent [regexp "^\{" $jsonette]
        incr indent $incr_indent
        lappend output  [string repeat "    " $indent][expr { $incr_indent ? "" : " " }]${jsonette}
        incr indent  [expr {[regexp -all "\{" $jsonette]-$incr_indent-[regexp -all "\}" $jsonette]}]
    }
    return [join $output \n]
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: