Tech Tip: New ways to create objects and collections
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: July 11, 2023
Starting in v20, the {} and [] operators can be used to create object and collection literals, respectively.
For example, instead of using New object and New collection like in the following:
$nestedObj:=New object $coll:=New collection(1; 2; 3) $nestedObj.foo:=$coll $obj:=New object("baz"; 4;"qux"; "wer") $nestedObj.bar:=$obj |
The above can simply be written as below:
$nestedObj:={foo: [1; 2; 3]; bar: {baz: 4; qux: "wer"}} |
Both ways will create the same nested object:
{
“foo”: [1,2,3],
“bar”: {
“baz”: 4,
“qux”: "wer"
}
}
As shown above, object and collection literals can be useful for writing nested JSON objects.
Note that key-value pairs in object literals, and values in collection literals, must be separated by semi-colon (;).
Also note that key names in object literals are not surrounded by quotations ("").