util::json::object::set_by_path (public)

 util::json::object::set_by_path -object object -path path -value value

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

This is an odd utility that mimics some odd code in the Ilias SCORM module, included here because it might be of more general use. Essentially we walk down an object tree structure using the "path" parameter. If we encounter a leaf on the way, we replace it with a new object node and continue. The last element of the path is interpreted as a leaf of the tree and is set to "value". Example: util::json::gen [util::json::object::set_by_path -object "" -path {a b c} -value 3] Result: {"a":{"b":{"c":3}}} Example: util::json::gen [util::json::object::set_by_path -object [util::json::object::create [list a [util::json::object::create [list d null]]]] -path {a b c} -value 3] Result: {"a":{"b":{"c":3},"d":null}} "a" is the top level object with two subnodes "b" and "d", with "b" having a subnode "c" of value 3, and "d" being a leaf of "a" with value "null".

Switches:
-object
(required)
The object to add subnodes to.
-path
(required)
The path through the tree with the last value being the name of a new or existing leaf.
-value
(required)
The value to set the final leaf to.
Returns:
A new object with the new tree structure interwoven into it.

Partial Call Graph (max 5 caller/called nodes):
%3 util::json::object::create util::json::object::create (public) util::json::object::get_values util::json::object::get_values (public) util::json::object::set_by_path util::json::object::set_by_path util::json::object::set_by_path->util::json::object::create util::json::object::set_by_path->util::json::object::get_values

Testcases:
No testcase defined.
Source code:
    if { [llength $object] < 2 } {
        array set values ""
    } else {
        array set values [util::json::object::get_values $object]
    }
    if { [llength $path] == 0 } {
        return $value
    } else {
        if { ![info exists values([lindex $path 0])] } {
            set values([lindex $path 0]) ""
        }
        set values([lindex $path 0])  [util::json::object::set_by_path  -object $values([lindex $path 0])  -path [lrange $path 1 end]  -value $value]
        return [util::json::object::create [array get values]]
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: