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

jsonset Operator (Examples)

4th December 2023 at 11:50am

The examples below assume the following JSON object is contained in the variable object-a:

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

[<object-a>jsonset[d],[Jaguar]]

[<object-a>jsonset[d],[f],[Panther]]

[<object-a>jsonset[d],[f],[-1],[Elephant]]

[<object-a>jsonset[d],[f],[-2],[Elephant]]

[<object-a>jsonset[d],[f],[-4],[Elephant]]

[<object-a>jsonset[Panther]]
→ If only a single parameter is specified, it replaces the entire JSON object

[<object-a>jsonset[]]
→ If only a single blank parameter is specified, no changes are made to the JSON object

The examples below assume the following JSON object is contained in the variable object-b:

{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null]}}

[<object-b>jsonset[]]
→ If only a single blank parameter is specified, no changes are made to the JSON object

[<object-b>jsonset[],[Antelope]]
→ If the property to be set is blank, the entire JSON object is replaced

[<object-b>jsonset:number[],[not a number]]
→ invalid numbers are interpreted as zero

[<object-b>jsonset[id],[Antelope]]
→ nonexistent top level properties are added to the object

[<object-b>jsonset[missing],[id],[Antelope]]
→ nonexistent nested properties are are ignored

[<object-b>jsonset:notatype[id],[Antelope]]
→ invalid type suffix is interpreted as the default string type

[<object-b>jsonset:boolean[id],[false]]

[<object-b>jsonset:boolean[id],[Antelope]]
→ invalid boolean value causes no assignment to be made

[<object-b>jsonset:number[id],[42]]

[<object-b>jsonset:null[id]]

[<object-b>jsonset:array[d],[f],[5]]

[<object-b>jsonset:object[d],[f],[5]]

[<object-a>] [<object-b>] :and[jsonset[b],[two]]
→ If the input consists of multiple JSON objects with matching properties, the value is set for all of them