Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read and write JSON

1,342 views
Skip to first unread message

Torsten Berg

unread,
May 12, 2017, 5:06:55 AM5/12/17
to
Hi,

I am using the tcllib package json to read a JSON file produced by the marvelous Pandoc document converter. After manipulating the dict returned by json::json2dict I want to convert the dict back to json.

However, I cannot find such a function in the json package. The tcllib json::write package doesn't seem to do the job either.

Am I missing something or which package/function do I need to use to convert my data back to JSON format?

Torsten

Georgios Petasis

unread,
May 12, 2017, 5:52:47 AM5/12/17
to
Well, you cannot. The problem is than when converting json to a tcl
dict, information about the value types is lost.

My suggestion is to use another json package. I think your best chance
is rl_json: https://github.com/RubyLane/rl_json

You can generate json from a dict, if you provide a template, and you
can parse json into an internal representation stored as a new tcl
object type. This means you can change a json value, without converting
into a tcl dict first.

For example:

set json {some json data}

With rl_json::json get $json ?key1? ?key2? ...

you can get dict functionality without converting json into a dict.

And with:
rl_json::json set json key1 key2 value

You can modify what is in the json variable. When uou ask for the string
of the json variable, it will always be valid json data (even if you
modify it).

Just using "rl_json::json get {some json data}" is the equivalent of
json::json2dict. Also, in some simple measurements I have done (for
generating json), rl_json was faster than tcllib's json::write.

George

Rich

unread,
May 12, 2017, 7:40:34 AM5/12/17
to
Torsten Berg <be...@typoscriptics.de> wrote:
> ... After manipulating the dict
> returned by json::json2dict I want to convert the dict back to json.
>
> However, I cannot find such a function in the json package. The
> tcllib json::write package doesn't seem to do the job either.
>
> Am I missing something or which package/function do I need to use to
> convert my data back to JSON format?

Because the required type information that JSON contains is lost when
converting into a Tcl dict, there is no "dict2json" command.

To use Tcllib's json::write you need to rebuild the json structure
using the available commands there, because each command reapplies the
required "type" info (the proper punctuation) to each value.

Gerald W. Lester

unread,
May 12, 2017, 10:04:35 AM5/12/17
to
As Georgios and Rich noted, when going from JSON to a dictionary
type/structure information is lost. The same is true when going form XML to
a dictionary.

For one "solution" on how this can be solved, take a look at the TclWS
package -- in particular the server side definitions of type structures.
While this applies to (currently) to XML, very small modifications (once the
TDOM parses/generates JSON, which is being worked on) could have it
read/generate JSON. NOTE -- SOAP does not support JSON, but the package can
do things "beyond" strict SOAP.

--
+------------------------------------------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

Torsten Berg

unread,
May 12, 2017, 4:55:59 PM5/12/17
to
Dear all,

thanks for all your answers. That is all very good advice. I will try rl_json now, as this solution seems a very convenient way of manipulating specific parts of the JSON document easily. I just need to compile it on my Mac yet.

Torsten

Mark Tarver

unread,
May 21, 2017, 2:13:37 PM5/21/17
to
Sam Chase did a Json package for parsing Json.

https://github.com/samebchase/shen-json

Mark

Torsten Berg

unread,
May 31, 2017, 5:12:32 PM5/31/17
to
Thanks, parsing is not the problem. The tcllib package can also do this. I am interested in manipulating the json and writing it back to a file in json format. I am currently evaluating rl_json which seems well-suited for this.
0 new messages