- Publicity: Public Only All
json-procs.tcl
Utility ad_procs for Tcl <-> JSON conversion. This is based on the tcllib json package written by Andreas Kupries, and later rewritten to parse via regular expressions by Thomas Maeder. The tcllib version suffers from generating Tcl structures from JSON strings with no type (JSON array or object) information. The resulting structures can't be converted back to JSON strings, you have to munge them with type information first. And the code making use the Tcl structure also needs to know whether each field is an object or array. It also depends on the DICT package or Tcl 8.5. This rewrite doesn't depend on DICT, declares procs using ad_proc (so the API will be picked up by our API browser), and is symmetrical (you can convert from JSON to the Tcl representation and back again). I've not renamed internal variables in the typical OpenACS style. I've placed these in the global util namespace for two reasons: 1. Don't want to clash with the tcllib json package in case someone else decides to use it. 2. Might put it in acs-tcl as part of the utility stuff someday. More information ... See http://www.json.org/ && http://www.ietf.org/rfc/rfc4627.txt Total rework of the code published with version number 1.0 by Thomas Maeder, Glue Software Engineering AG
- Location:
- packages/acs-tcl/tcl/json-procs.tcl
- Created:
- 2010/04/09
- Author:
- Don Baccus
- CVS Identification:
$Id: json-procs.tcl,v 1.18 2026/12/05 13:20:20 gustafn Exp $
Procedures in this file
- util::json2dict (public)
- util::json::array::create (public, deprecated)
- util::json::array::get_values (public, deprecated)
- util::json::gen (public, deprecated)
- util::json::indent (public, deprecated)
- util::json::json_value_to_sql_value (public, deprecated)
- util::json::object::create (public, deprecated)
- util::json::object::get_value (public, deprecated)
- util::json::object::get_values (public, deprecated)
- util::json::object::set_by_path (public, deprecated)
- util::json::object::set_value (public, deprecated)
- util::json::parse (public, deprecated)
- util::json::sql_values_to_json_values (public, deprecated)
- util::json::type_of (public, deprecated)
- util::tdomDoc2dict (public)
Detailed information
util::json2dict (public)
util::json2dict jsonText
Parse JSON text into Tcl values, using NaviServer's ns_json when available and tDOM otherwise. JSON objects become dictionaries, arrays become lists, and null is returned as the string "null" (also, when ns_json is used, for tdmom compatibility).
- Parameters:
- jsonText (required)
- JSON text
- Returns:
- The Tcl representation of the JSON value
- Author:
- Gustaf Neumann
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- json_to_dict
util::json::array::create (public, deprecated)
util::json::array::create values
Deprecated. Invoking this procedure generates a warning.
For new code, use a Tcl list for untyped values or build indexed typed triples for ns_json value -type array. Construct a JSON object with the given values list
- Parameters:
- values (required)
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::array::get_values (public, deprecated)
util::json::array::get_values item
Deprecated. Invoking this procedure generates a warning.
For new code, use Tcl list operations on values from ns_json parse, or use ns_json triples getvalue for typed data. Verify that the given Tcl structure is an object, and return its values list.
- Parameters:
- item (required)
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::gen (public, deprecated)
util::json::gen value
Deprecated. Invoking this procedure generates a warning.
Generate JSON from the legacy tagged Tcl list format. Prefer ns_json value with explicit JSON types for new code. When ns_json is available, this wrapper encodes the legacy values with the native serializer. Strings that look like JSON numbers, booleans or null are still inferred as those types.
- Parameters:
- value (required)
- A two-element object/array Tcl list.
- Returns:
- A JSON string.
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- legacy_json_generation
util::json::indent (public, deprecated)
util::json::indent [ -simplify ] json
Deprecated. Invoking this procedure generates a warning.
Indent a JSON string using the legacy formatter. When ns_json is available, use this type-preserving alternative:
set triples [ns_json parse -output triples -- $json] ns_json value -pretty -- $triplesThe native formatter preserves all fields. The legacy -simplify option removes fields and has no direct equivalent.
- Switches:
- -simplify (optional, boolean)
- If true, remove all fields that don't contribute to the structure of the object/array combination being described by the string.
- Parameters:
- json (required)
- The string to indent
- Returns:
- The beautifully indented, and optionally simplified, string
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::json_value_to_sql_value (public, deprecated)
util::json::json_value_to_sql_value value
Deprecated. Invoking this procedure generates a warning.
For new code, use database bind variables and map JSON booleans and null to the types expected by the target column. While mysql happily treats false as 0, real SQL does not. And we need to protect against apostrophes in strings. And handle null. You get the idea.
- Parameters:
- value (required)
- A value from a parsed JSON string
- Returns:
- Something that works in Real SQL, not to be confused with MySQL. This includes not trying to insert '' into columns of type real, when "null" is meant (we mimic Oracle bindvar/PG bindvar emulation semantics). The Ilias RTE JavaScript returns '' rather than null for JS null variables.
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::object::create (public, deprecated)
util::json::object::create values
Deprecated. Invoking this procedure generates a warning.
For new code, use a Tcl dictionary for untyped values or build named typed triples for ns_json value -type object. Construct a JSON object with the given values list
- Parameters:
- values (required)
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::object::get_value (public, deprecated)
util::json::object::get_value -object object -attribute attribute
Deprecated. Invoking this procedure generates a warning.
For new code, use dict get on Tcl values or ns_json triples getvalue on typed JSON data. Returns the value of an attribute in an object. If the attribute doesn't exist, an error will result.
- Switches:
- -object (required)
- The JSON object which contains the attribute.
- -attribute (required)
- The attribute name.
- Returns:
- The attribute value or an error, if the attribute doesn't exist.
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::object::get_values (public, deprecated)
util::json::object::get_values item
Deprecated. Invoking this procedure generates a warning.
For new code, use Tcl dictionary operations on values from ns_json parse, or use ns_json triples getvalue for typed data. Verify that the given Tcl structure is an object, and return its values list.
- Parameters:
- item (required)
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::object::set_by_path (public, deprecated)
util::json::object::set_by_path -object object -path path -value value
Deprecated. Invoking this procedure generates a warning.
For new code, consider ns_json triples setvalue on typed JSON data. The legacy behavior of replacing an intermediate leaf with an object must be handled explicitly when migrating. 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):
- Testcases:
- No testcase defined.
util::json::object::set_value (public, deprecated)
util::json::object::set_value -object object -attribute attribute \
-value valueDeprecated. Invoking this procedure generates a warning.
For new code, use dict set on Tcl values or ns_json triples setvalue on typed JSON data. Set an attribute value in an object structure. If the attribute doesn't exist in the object, it's created.
- Switches:
- -object (required)
- The object we want to set the value in.
- -attribute (required)
- The name of the attribute.
- -value (required)
- The value to set attribute to.
- Returns:
- A new object with the attribute/value pair.
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::parse (public, deprecated)
util::json::parse jsonText
Deprecated. Invoking this procedure generates a warning.
Parse JSON text into the legacy tagged Tcl list format. Use ns_json parse for ordinary Tcl values, or ns_json parse -output triples when JSON types must be preserved. Use util::json2dict for a portable Tcl-value conversion with a tDOM fallback. These alternatives return different representations from the legacy tagged list.
- Parameters:
- jsonText (required)
- JSON text
- Returns:
- List containing the object represented by jsonText
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::sql_values_to_json_values (public, deprecated)
util::json::sql_values_to_json_values row
Deprecated. Invoking this procedure generates a warning.
For new JSON output, represent SQL NULL as an explicit null type in the triples passed to ns_json value. This legacy helper maps all empty strings to null. Converts empty values to "null", consistent with how oracle, mysql, and the nspostgres bindvar hack treats them.
- Parameters:
- row (required)
- A row (list) returned by a sql SELECT.
- Returns:
- A new list with empty strings converted to null.
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::json::type_of (public, deprecated)
util::json::type_of item
Deprecated. Invoking this procedure generates a warning.
For new code, use ns_json triples gettype on typed JSON data. Return the type of the item, "object" or "array"
- Parameters:
- item (required)
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.
util::tdomDoc2dict (public)
util::tdomDoc2dict doc
Helper proc for util::json2dict, which outputsreturns the provided tDOM document in the form of a Tcl dict.
- Parameters:
- doc (required)
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- No testcase defined.