Hi *,
I'd like a method to retrieve the complete key structure from a
json string and I'm using the json tcllib module.
It may contain an unknown number of nested levels and json arrays.
I have found some solutions which work basically I cannot
distinguish reliable between a normal - maybe nested json
object - and a json array.
Hi *,
I'd like a method to retrieve the complete key structure from a
json string and I'm using the json tcllib module.
It may contain an unknown number of nested levels and json arrays.
I have found some solutions which work basically I cannot
distinguish reliable between a normal - maybe nested json
object - and a json array.
All found examples fail json array arrays.
The extracted or created keys may eventually be used for accessing the corresponding values - json does not create specific ids for the array entries, they are a list in the tcl's point of view.
Has anyone tried or mastered this challenge?
Here's a typical example:
---
https://www.tech-edv.co.at/download/testdata/livedata_20250914.txt
On 15/09/2025 17:58, Gerhard Reithofer wrote:
Hi *,
I'd like a method to retrieve the complete key structure from a
json string and I'm using the json tcllib module.
It may contain an unknown number of nested levels and json arrays.
I have found some solutions which work basically I cannot
distinguish reliable between a normal - maybe nested json
object - and a json array.
All found examples fail json array arrays.
The extracted or created keys may eventually be used for accessing the
corresponding values - json does not create specific ids for the array
entries, they are a list in the tcl's point of view.
Has anyone tried or mastered this challenge?
Here's a typical example:
---
https://www.tech-edv.co.at/download/testdata/livedata_20250914.txt
I'm by no means a JSON expert, in natural language terms I'd describe
myself as speaking schoolboy JSON, so I may have overlooked some
technical subtlety. As far as I can see
package require json
package require http
package require tls
http::register https 443 {tls::socket -autoservername true}
set tok [http::geturl
https://www.tech-edu.co.at/download/testdata/livedata_20290914.txt]
set jsStr [http::data $tok]
http::cleanup $tok
set jsDict [json::json2dict $jsStr]
delivers a completely usable dictionary.
dict get $jsDict inverters
returns a two-element list which is analogous, in many programming >languages, to an array with two valid indexes.
Sorry if I've missed something.
Alan
Gerhard Reithofer <[email protected]> wrote:
Hi *,
This is because the tcllib json module does not return any typing information, so you have to guess as to whether you have an "object" or
an "array" at any given level.
On 15/09/2025 17:58, Gerhard Reithofer wrote:
Hi *,
I'd like a method to retrieve the complete key structure from a
json string and I'm using the json tcllib module.
It may contain an unknown number of nested levels and json arrays.
delivers a completely usable dictionary.
dict get $jsDict inverters
returns a two-element list which is analogous, in many programming languages, to an array with two valid indexes.
In article <10abki9$2ift8$[email protected]>,
Alan Grunwald <[email protected]> wrote:
On 15/09/2025 17:58, Gerhard Reithofer wrote:
Hi *,
I'd like a method to retrieve the complete key structure from a
The disadvantage being that it's not a standard part of tcllib.
https://github.com/RubyLane/rl_json
...
The disadvantage being that it's not a standard part of tcllib.
https://github.com/RubyLane/rl_json
unfortunately I haven't found the complate documentation online - any
hint?
On Tue, 16 Sep 2025, Ted Nolan <tednolan> wrote:
Hi Ted,
In article <10abki9$2ift8$[email protected]>,
Alan Grunwald <[email protected]> wrote:
On 15/09/2025 17:58, Gerhard Reithofer wrote:
Hi *,
I'd like a method to retrieve the complete key structure from a
...
The disadvantage being that it's not a standard part of tcllib.
https://github.com/RubyLane/rl_json
unfortunately I haven't found the complate documentation online - any
hint?
On Tue, 16 Sep 2025, Ted Nolan <tednolan> wrote:
Hi Ted,
In article <10abki9$2ift8$[email protected]>,
Alan Grunwald <[email protected]> wrote:
On 15/09/2025 17:58, Gerhard Reithofer wrote:
Hi *,
I'd like a method to retrieve the complete key structure from a
...
The disadvantage being that it's not a standard part of tcllib.
https://github.com/RubyLane/rl_json
unfortunately I haven't found the complate documentation online - any
hint?
BTW I think that this problem can be solved with various tools, I have
only tried it with tcllib json and I'm a fan of tcl-only
implementations because tcl is availale on many platform and then you
need not to make anything except installing of tcl and if necessary
copy a bunch of files.
Thank you,
Gerhard
On Tue, 16 Sep 2025, Ted Nolan <tednolan> wrote:
Hi Ted,
In article <10abki9$2ift8$[email protected]>,
Alan Grunwald <[email protected]> wrote:
On 15/09/2025 17:58, Gerhard Reithofer wrote:
Hi *,
I'd like a method to retrieve the complete key structure from a
...
The disadvantage being that it's not a standard part of tcllib.
https://github.com/RubyLane/rl_json
unfortunately I haven't found the complate documentation online - any
hint?
BTW I think that this problem can be solved with various tools, I have
only tried it with tcllib json and I'm a fan of tcl-only
implementations because tcl is availale on many platform and then you
need not to make anything except installing of tcl and if necessary
copy a bunch of files.
Thank you,
Gerhard
On 9/16/2025 12:50 PM, Gerhard Reithofer wrote:
On Tue, 16 Sep 2025, Ted Nolan <tednolan> wrote:
Hi Ted,
I posed this problem to the claude AI, and it agrees with your findings of loss of type info. Claude's suggestion was to preparse the json string to find
array keys, and supplied the following code using regex's:
package require json
proc findArrayKeys {jsonString {path {}}} {
set arrayKeys {}
# Remove whitespace for easier parsing
set json [string map {"\n" "" "\t" "" " " " "} $jsonString]
# Find "key": [ patterns (arrays)
set pattern {"([^"]+)"\s*:\s*\[}
set start 0
while {[regexp -start $start -indices $pattern $json match keyIndices]} {
set key [string range $json {*}$keyIndices]
if {$path ne ""} {
lappend arrayKeys "$path.$key"
} else {
lappend arrayKeys $key
}
set start [expr {[lindex $match 1] + 1}]
}
return $arrayKeys
}
# Example usage
set jsonData {{"items": ["a", "b", "c"], "single": "hello", "nested": {"subitems": ["x", "y"]}}}
set parsed [::json::json2dict $jsonData]
set arrayKeys [findArrayKeys $jsonData]
puts "Array keys: $arrayKeys"
foreach {key value} $parsed {
if {$key in $arrayKeys} {
puts "$key is an array: $value"
} else {
puts "$key is not an array: $value"
}
}
On Tue, 16 Sep 2025, et99 wrote:
On 9/16/2025 12:50 PM, Gerhard Reithofer wrote:
On Tue, 16 Sep 2025, Ted Nolan <tednolan> wrote:
Hi Ted,
...
I posed this problem to the claude AI, and it agrees with your findings of >> loss of type info. Claude's suggestion was to preparse the json string to find
array keys, and supplied the following code using regex's:
package require json
proc findArrayKeys {jsonString {path {}}} {
set arrayKeys {}
# Remove whitespace for easier parsing
set json [string map {"\n" "" "\t" "" " " " "} $jsonString]
# Find "key": [ patterns (arrays)
set pattern {"([^"]+)"\s*:\s*\[}
set start 0
while {[regexp -start $start -indices $pattern $json match keyIndices]} {
set key [string range $json {*}$keyIndices]
if {$path ne ""} {
lappend arrayKeys "$path.$key"
} else {
lappend arrayKeys $key
}
set start [expr {[lindex $match 1] + 1}]
}
return $arrayKeys
}
# Example usage
set jsonData {{"items": ["a", "b", "c"], "single": "hello", "nested":
{"subitems": ["x", "y"]}}}
set parsed [::json::json2dict $jsonData]
set arrayKeys [findArrayKeys $jsonData]
puts "Array keys: $arrayKeys"
foreach {key value} $parsed {
if {$key in $arrayKeys} {
puts "$key is an array: $value"
} else {
puts "$key is not an array: $value"
}
}
Really interesting approach good looks good.
Not mentioning that all entities must be reparsed recursively it could
be a solution.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,070 |
Nodes: | 10 (0 / 10) |
Uptime: | 171:03:55 |
Calls: | 13,735 |
Calls today: | 1 |
Files: | 186,967 |
D/L today: |
182 files (54,273K bytes) |
Messages: | 2,419,004 |