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

converting keyed list to JSON

1,062 views
Skip to first unread message

pat...@gmail.com

unread,
May 29, 2014, 5:25:11 PM5/29/14
to
Hello, has anyone written any snippets to convert from keyed list to JSON? I tried to use the JSON::dict2json and browsing at the code, it may not work well.

I'm unsure how json::write works and/or if there are any examples of how this could be used for keyed lists.

Thanks for any inputs.

Rich

unread,
May 29, 2014, 6:15:21 PM5/29/14
to
pat...@gmail.com wrote:
> Hello, has anyone written any snippets to convert from keyed list to
> JSON?

By "keyed list" do you actually mean a dict?

> I tried to use the JSON::dict2json and browsing at the code, it may
> not work well.

Was this the tcllib json module? What did you find that may not have
worked well?

> I'm unsure how json::write works and/or if there are any examples of
> how this could be used for keyed lists.

There's general JSON info on the wiki here: http://wiki.tcl.tk/13419

And the wiki page for the tcllib JSON module is here:
http://wiki.tcl.tk/40053, including some examples.

The tcllib module also ships with a manpage, so you could also read the
manpage. Since you appear to want to generate JSON, you should
probably read the json_write manpage first.

Gerald W. Lester

unread,
May 29, 2014, 7:01:46 PM5/29/14
to
On 5/29/14, 4:25 PM, pat...@gmail.com wrote:
> Hello, has anyone written any snippets to convert from keyed list to JSON? I tried to use the JSON::dict2json and browsing at the code, it may not work well.

Like the other poster we assumed by keyed list you really mean dictionary.
Where is the JSON::dict2json that you tried? I know of a ::json::json2dict
but no JSON::dict2json.

> I'm unsure how json::write works and/or if there are any examples of how this could be used for keyed lists.

You need to build a special dictionary (.e.g. jsonFormattedDict) consisting
of the keys (single level) plus the value formatted for json (use
[::json::write string] to format it) then use [::json::write object
{*}jsonFormattedDict] to convert it to a JSON object.

All of that is right out of the man/help page.

--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

pat...@gmail.com

unread,
May 29, 2014, 7:33:50 PM5/29/14
to
Keyed Lists are described here: http://www2.lib.uchicago.edu/keith/tcl-course/topics/lists.html

We are using them since they are fully supported in Tcl 8.4.

Is the below the correct code for converting dict to JSON? Or is there a more updated and less error prone converter somewhere?

proc dict2json {dictionary} {
dict for {key value} $dictionary {
if {[string match {\[*\]} $value]} {
lappend Result "\"$key\":$value"
} elseif {![catch {dict size $value}]} {
lappend Result "\"$key\":\"[dict2json $value]\""
} else {
lappend Result "\"$key\":\"$value\""
}
}
return "\{[join $Result ",\n"]\}"
}

I will have to first convert the KL to a dict then to use this, right?

Thanks for any inputs!

Steve Havelka

unread,
May 29, 2014, 7:46:27 PM5/29/14
to
On 05/29/2014 04:33 PM, pat...@gmail.com wrote:
> Keyed Lists are described here: http://www2.lib.uchicago.edu/keith/tcl-course/topics/lists.html

Wow! That page is 20 years old. And keyed lists were before my time,
and I started using Tcl 10 years ago.


>
> We are using them since they are fully supported in Tcl 8.4.

Is there a reason you can't use 8.5 or 8.6?







>
> Is the below the correct code for converting dict to JSON? Or is there a more updated and less error prone converter somewhere?
>
> proc dict2json {dictionary} {
> dict for {key value} $dictionary {
> if {[string match {\[*\]} $value]} {
> lappend Result "\"$key\":$value"
> } elseif {![catch {dict size $value}]} {
> lappend Result "\"$key\":\"[dict2json $value]\""
> } else {
> lappend Result "\"$key\":\"$value\""
> }
> }
> return "\{[join $Result ",\n"]\}"
> }
>
> I will have to first convert the KL to a dict then to use this, right?
>
> Thanks for any inputs!
>


--
Steve Havelka <smh...@gmail.com>
Proprietor of http://TclForums.info

Rich

unread,
May 29, 2014, 9:01:15 PM5/29/14
to
pat...@gmail.com wrote:
> Keyed Lists are described here: http://www2.lib.uchicago.edu/keith/tcl-course/topics/lists.html

> We are using them since they are fully supported in Tcl 8.4.

Keyed lists are not supported in any Tcl. They are supported by the
TclX extension (http://wiki.tcl.tk/207). That is not "Tcl" but "TclX".

However, from the looks of the course material you provided, keyed
lists and dicts are of identical structure.

> Is the below the correct code for converting dict to JSON? Or is
> there a more updated and less error prone converter somewhere?

> proc dict2json {dictionary} {
> dict for {key value} $dictionary {
> if {[string match {\[*\]} $value]} {
> lappend Result "\"$key\":$value"
> } elseif {![catch {dict size $value}]} {
> lappend Result "\"$key\":\"[dict2json $value]\""
> } else {
> lappend Result "\"$key\":\"$value\""
> }
> }
> return "\{[join $Result ",\n"]\}"
> }

Possibly. Although I don't understand why you are matching against
literal square brackets in the string match. I'm not saying its wrong,
just that I don't immediately see the reason why, which likely reflects
the fact that I've done almost nothing with JSON.

> I will have to first convert the KL to a dict then to use this, right?

From the looks of the course docs, a KL is a dict, so conversion is as
simple as using the KL variable as if it were a dict.

pat...@gmail.com

unread,
May 29, 2014, 9:11:21 PM5/29/14
to
That routine isn't mine, but I was inquiring if anyone used that for Keyed lists or for dictionaries?

who maintains the json packages?

Due to the vastness of the legacy code we have, I am forced to remain in Tcl 8.4 for a while.

Thanks folks for the quick replies.

Steve Havelka

unread,
May 29, 2014, 9:15:55 PM5/29/14
to
On 05/29/2014 06:11 PM, pat...@gmail.com wrote:
> That routine isn't mine, but I was inquiring if anyone used that for
> Keyed lists or for dictionaries?
>
> who maintains the json packages?
>
> Due to the vastness of the legacy code we have, I am forced to remain
> in Tcl 8.4 for a while.


What code do you have that won't work on a version of Tcl after 8.4?





>
> Thanks folks for the quick replies.
>
>
> On Thursday, May 29, 2014 5:25:11 PM UTC-4, pat...@gmail.com wrote:
>> Hello, has anyone written any snippets to convert from keyed list
>> to JSON? I tried to use the JSON::dict2json and browsing at the
>> code, it may not work well.
>>
>>
>>
>> I'm unsure how json::write works and/or if there are any examples
>> of how this could be used for keyed lists.
>>
>>
>>
>> Thanks for any inputs.
>


Rich

unread,
May 29, 2014, 9:30:15 PM5/29/14
to
pat...@gmail.com wrote:
> On Thursday, May 29, 2014 5:25:11 PM UTC-4, pat...@gmail.com wrote:
> > Hello, has anyone written any snippets to convert from keyed list
> > to JSON? I tried to use the JSON::dict2json and browsing at the
> > code, it may not work well.
> >
> > I'm unsure how json::write works and/or if there are any examples
> > of how this could be used for keyed lists.
> >
> > Thanks for any inputs.

> That routine isn't mine, but I was inquiring if anyone used that for
> Keyed lists or for dictionaries?

Never saw it before, so can't comment further.

> who maintains the json packages?

Don't know, check the fossil change logs for tcllib.

> Due to the vastness of the legacy code we have, I am forced to remain
> in Tcl 8.4 for a while.

Have you tested any of your legacy code to determine if an upgrade
works without issue. Tcl is remarkably forward compatible. I have
code I wrote 15+ years ago (originally in a Tcl prior to 8.4) that
still works perfectly, unchanged, in 8.6.


Note, is you must followup via google groups, please clean up the quoting
mess that google makes before you post your followup.

Donal K. Fellows

unread,
May 30, 2014, 9:14:39 AM5/30/14
to
On 30/05/2014 02:01, Rich wrote:
> However, from the looks of the course material you provided, keyed
> lists and dicts are of identical structure.

They are not (alas, and unavoidably[*]). The dictionary:
1 abc 2 def 3 pqr 4 xyz
is equivalent logically to the keyed list:
{1 abc} {2 def} {3 pqr} {4 xyz}

Keyed lists support composite keys (e.g., "abc.def"), dictionaries use
multiple arguments (so that keys may be arbitrary strings) and have
rather more different support subcommands.

Donal.
[* I went with the model of values used with [array get]/[array set] as
that had — and still has — even more traction. ]
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.

Lawrence Woodman

unread,
Jun 1, 2014, 2:39:44 AM6/1/14
to
On Thu, 29 May 2014 14:25:11 -0700, patna73 wrote:

> Hello, has anyone written any snippets to convert from keyed list to
> JSON? I tried to use the JSON::dict2json and browsing at the code, it
> may not work well.

I'm surprised that you came across dict2json as it isn't documented. It
doesn't work in any case because JSON needs to know the type of data you
are working with. What you really need to do is create a custom proc to
convert a dict to JSON when you know how it is formatted. As so:

package require json
package require json::write

proc qualifications2Json {qualifications} {
set res {}
foreach qualification $qualifications {
lappend res [json::write string $qualification]
}
json::write array {*}$res
}

proc employeeDict2Json {employeeDict} {
set name [json::write string [dict get $employeeDict name]]
set age [dict get $employeeDict age]
set qualifications \
[qualifications2Json [dict get $employeeDict qualifications]]
json::write object name $name age $age qualifications $qualifications
}

set employeeJson {
{ "name": "Fred Smith",
"age": 28,
"qualifications": ["BSc", "MSc", "Phd"] }}

set employeeDict [::json::json2dict $employeeJson]

puts "employeeJson: $employeeJson\n"
puts "employeeDict: $employeeDict\n\n"
puts "dict2json fails as it doesn't quote the name, nor create a proper \
array for qualifications"
puts "dict2json: [::json::dict2json $employeeDict]\n"
puts "employeeDict2Json: [employeeDict2Json $employeeDict]\n"
puts "employeeDict2Json -> json2Dict: \
[::json::json2dict [employeeDict2Json $employeeDict]]\n"




That will produce the following output:

employeeJson:
{ "name": "Fred Smith",
"age": 28,
"qualifications": ["BSc", "MSc", "Phd"] }

employeeDict: name {Fred Smith} age 28 qualifications {BSc MSc Phd}


dict2json fails as it doesn't quote the name, nor create a proper array
for qualifications
dict2json: {"name": Fred Smith,
"age": 28,
"qualifications": BSc MSc Phd,
}

employeeDict2Json: {
"name" : "Fred Smith",
"age" : 28,
"qualifications" : ["BSc","MSc","Phd"]
}

employeeDict2Json -> json2Dict: name {Fred Smith} age 28 qualifications
{BSc MSc Phd}




This isn't in the best style as I haven't done any Tcl for about six
months so I'm a bit rusty, but I hope that it helps.


Lorry

--
vLife Systems Ltd
Registered Office: The Apex, 2 Sheriffs Orchard, Coventry, CV1 3PP
Registered in England and Wales No. 06477649
http://vlifesystems.com

Gerald W. Lester

unread,
Jun 1, 2014, 3:40:44 PM6/1/14
to
On 6/1/14, 1:39 AM, Lawrence Woodman wrote:
> On Thu, 29 May 2014 14:25:11 -0700, patna73 wrote:
>
>> Hello, has anyone written any snippets to convert from keyed list to
>> JSON? I tried to use the JSON::dict2json and browsing at the code, it
>> may not work well.
>
> I'm surprised that you came across dict2json as it isn't documented. It
> doesn't work in any case because JSON needs to know the type of data you
> are working with. What you really need to do is create a custom proc to
> convert a dict to JSON when you know how it is formatted. ...

You may want to take a look at the ODataTclOO package. It uses ATOM format
instead of JSON, but you could use it as a go-by with the ::json::write package.
0 new messages