Another alternative is to use the json package that is part of Tcllib. It includes a parser for JSON. Very easy to use.
However, the JSON text is converted to a Tcl dictionary. Therefore, if you are on Tcl 8.4, you will also have to download and install the “dict” package for 8.4, besides Tcllib.
package require json
Then use the command “::json::json2dict” to parse your JSON text into a Tcl dict.
Example from the command line:
% package require json
1.1.2
% set jsontxt {[
{
“id”: 2,
“name”: “An ice sculpture”,
“price”: 12.50,
“tags”: [”cold”, “ice”],
“dimensions”: {
“length”: 7.0,
“width”: 12.0,
“height”: 9.5
},
“warehouseLocation”: {
“latitude”: -78.75,
“longitude”: 20.4
}
},
{
“id”: 3,
“name”: “A blue mouse”,
“price”: 25.50,
“dimensions”: {
“length”: 3.1,
“width”: 1.0,
“height”: 1.0
},
“warehouseLocation”: {
“latitude”: 54.4,
“longitude”: -32.7
}
}
]
}
% ::json::json2dict $jsontxt
{id 2 name {An ice sculpture} price 12.50 tags {cold ice} dimensions {length 7.0 width 12.0 height 9.5} warehouseLocation {latitude -78.75 longitude 20.4}} {id 3 name {A blue mouse} price 25.50 dimensions {length 3.1 width 1.0 height 1.0} warehouseLocation {latitude 54.4 longitude -32.7}}
Once your JSON is parsed into a dictionary, you use the regular dict commands to retrieve the desired data elements.
I hope this helps.