Forum OpenACS Development: JSON Array & objects

Request notifications

Posted by Iuri Sampaio on
Hi there,

Retrieving a JSON, as in (1), and parsing the result, as in (2) results in a huge and cluttered list os lists, as in (4)
I did even tried to convert it to an array, but it is still painful to manipulate.

Is there a clearer and more direct way to handle JSON results?

(1) set json [im_httpget $url]

(2) set parsed_result [util::json::parse $json]

(3) array set result_hash [lindex $parsed_result 1]

(4) List of lists
[31/Jan/2018:11:35:12][2066.7ffac156f700][-conn:evex:0-] Notice: _object_ {html_attributions {_array_ {}} results {_array_ {{_object_ {geometry {_object_ {location {_object_ {lat -33.8581392 lng 151.2098175}} viewport {_object_ {northeast {_object_ {lat -33.8567902197085 lng 151.2111664802915}} southwest {_object_ {lat -33.8594881802915 lng 151.2084685197085}}}}}} icon id 8e980ad0c819c33cdb1cea31e72d654ca61a7065 name {Cruise Bar, Restaurant & Events} opening_hours {_object_ {open_now false weekday_text {_array_ {}}}} photos {_array_ {{_object_ {height 1134 html_attributions {_array_ {{Cruise Bar, Restaurant & Events}}} photo_reference CmRaAAAA7AvF1ITVw4Ik99bkK8TULBeYRkLW9EzbZjKn_OzS0UDld5VKeGWVA6kne9xZyOQ9jmZjmJcRIaqcBPBzjqMNlD61RZZgsZ_OFe_wCAInrRcaObqqhUS3AsBZXumqiC4gEhA7JElNjdAfFANdvKycnmykGhQtBDlNmvG2IqRoYKiOvqo5dzqDcQ width 2048}}}} place_id ChIJi6C1MxquEmsR9-c-3O48ykI price_level 1 rating 3.9 reference CmRRAAAA14SKShVcPvzOXip11-6N-69uE43uIcmxQuBh-0KZ32FoXY7QMD9ceLoZqYwWK2tDrlwrv5aNA6Z40JRO8Q-aL3GV5XmT1DuUOfyCB5TaV8j4NxVbUmsVSuaufvg0hVOrEhB0_hqJIkF_RJgzBwfybof0GhSzKWNcFTWa_w12ef0CJeq4Vy2UdA scope GOOGLE types {_array_ {bar restaurant food point_of_interest establishment}} vicinity {Circular Quay West, Overseas Passenger Terminal, Sydney}}} {_object_ {geometry {_object_ {location {_object_ {lat -33.8610965 lng 151.2097834}} viewport {_object_ {northeast {_object_ {lat -33.8597475197085 lng 151.2111323802915}} southwest {_object_ {lat -33.8624454802915 lng 151.2084344197085}}}}}} icon id 9ea7c77cb181b1f33d19c9d76121fcc6d5246ad8 name {Australian Cruise Group Circular Quay} opening_hours {_object_ {open_now false weekday_text {_array_ {}}}} photos {_array_ {{_object_ {height 1152 html_attributions {_array_ {{Australian Cruise Group Circular Quay}}} photo_reference CmRaAAAAH8Z4q3kIdWjfcptt6hySA0uuWFttlqL3cgl17sBjCaxH9FDk4vSccye3SpW3

2: Re: JSON Array & objects (response to 1)
Posted by Iuri Sampaio on
As JSON results may vary a lot, it seems there will be customization for every kind of result. Roughly, there will be a combination of "array sets" and "foreaches", which can be put into a recursive ad_proc.

Not sure if that will solve all cases, but at least it works for GoogleMapsAPI :: nearbies places.

Best wishes,

set parsed_result [util::json::parse $json]

array unset json_hash
array set json_hash [lindex $parsed_result 1]
array set json_hash $json_hash(results)

if {[info exists json_hash(_array_)]} {
set json_array $json_hash(_array_)
foreach array_elem $json_array {
set obj [lindex $array_elem 0]
set json_list [lindex $array_elem 1]

if {$obj eq "_array_" } {
# here it goes a recursive call

3: Re: JSON Array & objects (response to 1)
Posted by Gustaf Neumann on

For processing complex JSON data, have a look at tDOM 0.9.0 (, manual) or newer. These new versions of tDOM support among other nice things JSON parsing. One can do e.g.:

set json {{"string":"bar","number":1,"boolean":true,"array":[1,2,3],"object":{"foo":"bar","baz":"boo"}}}
dom parse -json $json doc something with $doc ...

to parse a JSON structure and use later XPath on the root node of $doc to extract whatever data you are interested is. This is very handy, especially with deeper nested structures.

4: Re: JSON Array & objects (response to 3)
Posted by Iuri Sampaio on
lol! Let me do my homework then! rsrs
5: Re: JSON Array & objects (response to 3)
Posted by Gustaf Neumann on

Have a look at rl_json, which supports parsing, accessing, iterating, ... and templating for JSON, very neat!

It also looks performance-wise very good (a comparison with other approaches from the RublyLane people):

-- parse-1.1: "Parse a small JSON doc and extract a field" --------------------
                   | This run
    old_json_parse |  241.595
     rl_json_parse |    5.540
       rl_json_get |    4.950
           yajltcl |    8.800
rl_json_get_native |    0.800

rl_json is available from