Forum OpenACS Development: Error: invalid command name "json::json2dict"

Request notifications

errror.log returns that error after running a TCL script

dom parse -json $json doc
# Converts JSON to TCL array
foreach tree $json {
set t [json::json2dict "\{$tree\}"]
}

rl_json namespace is in place

package require rl_json
namespace path {::rl_json}

TCL version installed is TCL8.6

Why does NS throw that error?

More Documentation: https://core.tcl-lang.org/tcllib/doc/tcllib-1-18/embedded/www/tcllib/files/modules/json/json.html

Log message - File Error.log

03/Nov/2019:21:36:45][8994.7f95caffd700][-conn:iurix:46:46124-] Error: invalid command name "json::json2dict"
while executing
"json::json2dict "\{$tree\}""
(procedure "::nsf::procs::template::import_ctrees" line 24)
invoked from within
"import_ctrees -url $url"
("uplevel" body line 1)
invoked from within
"uplevel {
ad_page_contract {}

# https://github.com/F4IF/ctree-demo/blob/mvp/firebase-export.json

package require rl_json
namespace path {::rl_..."
(procedure "code::tcl::/var/www/iurix//packages/ctree-core/www/import-sa..." line 2)
invoked from within
"code::tcl::$__adp_stub"
("uplevel" body line 12)
invoked from within
"uplevel {

Collapse
Posted by Iuri Sampaio on
https://wiki.tcl-lang.org/page/JSON
"OpenACS has its own take on handling JSON data, see [1 ]. From the intro:"

That affirmation, got me to wonder if I could parse easily a JSON file using OpenACS only (i.e. https://github.com/F4IF/ctree-demo/blob/mvp/firebase-export.json)

What's the status of JSON oacs library?
I browsed my /api-doc installation and I could see much)

Best wishes,
I

Collapse
Posted by Gustaf Neumann on
Iuri,

you seem to mix up the APIs of the various json implementations for Tcl. If you want to use rl_json, use "json get ..." [1]. If you want to use tdom to parse json, use "dom parse -json ..." [2]. If you want to use tcllib's json implementation, use "::json::json2dict ..." [3]. In your snippet above, you seem to use pieces of all of these.

The reason, you are seeing the error message 'invalid command name "json::json2dict"' is that "package require json" is missing (see [3]) is missing in your script.

-g

[1] https://github.com/RubyLane/rl_json
[2] http://www.tdom.org/index.html/doc/trunk/doc/dom.html
[3] https://core.tcl-lang.org/tcllib/doc/tcllib-1-18/embedded/www/tcllib/files/modules/json/json.html

Collapse
Posted by Iuri Sampaio on
Indeed. A total mess! I was writing [1], [2] and [3], in the same script in order to evaluate all methods to parse a json input, then I made confusion with variable names.

I must avoid that bu commenting the code I write. Thanks

Btw, after I've cleaned the snnipet and added "package require json", the error is gone. https://iurix.com/ctree/import-sample

Best wishes,
I

Collapse
Posted by Brian Fenton on
Hi Iuri

I have used this API in the past, but they are not reliable ( breaking badly if data contained double quotes, braces or slashes)
but it might be sufficient for your needs
https://openacs.org/api-doc/procs-file-view?path=packages/acs-tcl/tcl/json-procs.tcl

I think they could be patched up though.

Brian

Collapse
Posted by Gustaf Neumann on
yes, the "util::json::*" interface as part of acs-tcl should not be used anymore and should be deprecated, when we have a replacement.

The best candidate is IMHO tdom 0.9* with its json support, since tdom is already a prerequirement for all OpenACS installations. Maybe we should add some ad_json2dict (or similar with a namespace prefix) to OpenACS, where we can add the tcllib function as a fallback, when tdom 0.9+ is not available. ... then the old interface can be deprecated.

Collapse
Posted by Brian Fenton on
That sounds great, Gustaf! In the meantime, I must take a look at tdom's json support.

cheers
Brian

Collapse
Posted by Gustaf Neumann on

...or use in the meantime tcllib:


package require json
::json::json2dict  {{"firstName""Gustaf""secondName""Neumann""country""Austria"}}
I haven't used tDOM's JSON support in real life -- but will add that the performance of rl_json::* on larger documents has impressed me a lot. Particularly compared to poor options for parsing, type management and consistency offered by the combinations of json2dict/huddle in tcllib.