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

jsonset Operator

4th December 2023 at 11:52am
purposeset the value of a property in JSON objects
inputa selection of JSON objects
suffixdata type of the value to be assigned to the property
parameterone or more indexes of the property to modify, if applicable followed by the value to be assigned
outputthe JSON objects with the specified value assigned to the specified property

Learn more about how to use Filters

New in v5.3.2 The jsonset operator is used to set a property value in JSON strings. See JSON in TiddlyWiki for background. See also the following related operators:

  • jsonget to retrieve the values of a property in JSON data
  • 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

The type of the value to be assigned to the property can be optionally specified with a suffix:

  • string: default, the string is specified as the final operand
  • boolean: the boolean value is true if the final operand is the string "true" and false if the final operand is the string "false", any other value for the final string results prevents the property from being assigned
  • number: the numeric value is taken from the final operand, invalid numbers are interpreted as zero
  • json: the JSON string value is taken from the final operand, invalid JSON prevents the property from being assigned
  • object: an empty object is assigned to the property, the final operand is ignored
  • array: an empty array is assigned to the property, the final operand is ignored
  • null: the special value null is assigned to the property, the final operand is ignored

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 jsonset operator uses multiple parameters to specify the indexes of the property to set. When used to assign strings (default behaviour if no suffix is specified) the final operand is interpreted as the value to assign.

Negative indexes are counted from the end, so -1 means the last item, -2 the next-to-last item, and so on.

Indexes can be dynamically composed from variables and transclusions, e.g. [<jsondata>jsonset<variable>,{!!field},[0],{CurrentResult}].

In the special case where only a single parameter is defined, the operator replaces the entire input object with the the value of that parameter. If the single parameter is blank, the operation is ignored and no assignment takes place.

If the input consists of multiple JSON objects with matching properties, the value is set for all of them.

Examples