ztcl::json get
Gives the value at a path of a JSON text, with no full parse.
ztcl::json get ?-null VALUE? ?--? TEXT ?STEP ...?
Description
Walks TEXT along the STEP arguments and gives the Tcl value at the end of the path, as parse gives it. A step is a key of an object or a decimal index of an array. With no STEP it gives the value of the whole text.
The walk makes only the value at the end of the path. It checks the brackets of each value that it skips, and does not check the scalars in them, so get can read a text that valid rejects. A path with no value is an error with the code ZT JSON PATH, and a null at the end needs -null.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| TEXT | JSON text | required | The text. |
| STEP | key or index | none | A key of an object, or an index of an array. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
-null | string | none | Gives the value of a JSON null. |
-- | flag | none | Ends the options. |
-null VALUE
Gives the value of a JSON null.
A null at the end of the path, or in the value there, becomes VALUE.
% ztcl::json get -null N {{"a": null}} a
N% ztcl::json get {{"a": null}} a
json syntax error at line 1, column 7: null needs the option -null
-errorcode ZT JSON NULL--
Ends the options.
The next argument is TEXT, also when it starts with -.
% ztcl::json get -- {[5]} 0
5A flag has no value to check.
Returns
The Tcl value at the path.
Examples
Each example of this page runs after package require ztcl::json.
% ztcl::json get {{"a": {"b": [10, 20]}}} a b 1
20
% ztcl::json get {{"a": {"b": [10, 20]}}} a
b {10 20}
% ztcl::json get {{"a": 1}}
a 1
Errors
% ztcl::json get {{"a": 1}} z
json path "z" has no value
-errorcode ZT JSON PATH
% ztcl::json get {[1, 2]} 5
json path "5" has no value
-errorcode ZT JSON PATH
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1