This page is part of a static HTML representation of the TiddlyWiki at https://tiddlywiki.com/

jsonget Operator

11th June 2022 at 10:47am
purposeretrieve the value of a property from JSON strings
inputa selection of JSON strings
parameterone or more indexes of the property to retrieve
outputthe values of each of the retrieved properties

Learn more about how to use Filters

Introduced in v5.2.4 See JSON in TiddlyWiki for background.

The jsonget operator is used to retrieve values from JSON data as strings. See also the following related operators:

  • jsontype to retrieve the type of a JSON value
  • jsonindexes to retrieve the names of the fields of a JSON object, or the indexes of a JSON array
  • jsonextract to retrieve a JSON value as a string of JSON

Properties within a JSON object are identified by a sequence of indexes. In the following example, the value at [a] is one, and the value at [d][f][0] is five.

{
    "a": "one",
    "b": "",
    "c": "three",
    "d": {
        "e": "four",
        "f": [
            "five",
            "six",
            true,
            false,
            null
        ],
        "g": {
            "x": "max",
            "y": "may",
            "z": "maize"
        }
    }
}

The following examples assume that this JSON data is contained in a variable called jsondata.

The jsonget operator uses multiple parameters to specify the indexes of the property to retrieve:

[<jsondata>jsonget[a]] --> "one"
[<jsondata>jsonget[d],[e]] --> "four"
[<jsondata>jsonget[d],[f],[0]] --> "five"

New in v5.3.2 Negative indexes into an array are counted from the end, so -1 means the last item, -2 the next-to-last item, and so on:

[<jsondata>jsonget[d],[f],[-1]] --> null
[<jsondata>jsonget[d],[f],[-2]] --> false
[<jsondata>jsonget[d],[f],[-4]] --> "six"

Indexes can be dynamically composed from variables and transclusions:

[<jsondata>jsonget<variable>,{!!field},[0]]

Boolean values and null are returned as normal strings. The jsontype operator can be used to retrieve a string identifying the original type. Thus:

[<jsondata>jsontype[a]] --> "string"
[<jsondata>jsontype[d]] --> "object"
[<jsondata>jsontype[d],[f]] --> "array"
[<jsondata>jsontype[d],[f],[2]] --> "boolean"

Using the jsonget operator to retrieve an object or an array returns a list of the values. For example:

[<jsondata>jsonget[d],[f]] --> "five","six","true","false","null"
[<jsondata>jsonget[d],[g]] --> "max","may","maize"

The jsonindexes operator retrieves the corresponding indexes:

[<jsondata>jsonindexes[d],[f]] --> "0", "1", "2", "3", "4"
[<jsondata>jsonindexes[d],[g]] --> "x", "y", "z"

If the object or array contains nested child objects or arrays then the values are retrieved recursively and returned flattened into a list. For example:

[<jsondata>jsonget[d]] --> "four","five","six","true","false","null","max","may","maize"

A subtlety is that the special case of a single blank parameter is used to identify the root object. Thus:

[<jsondata>jsonindexes[]] --> "a", "b", "c", "d"