Back to Home
$jsonArray
$jsonArray marks a key in the current JSON object as an
empty array. Use it when you want to start building an array at a key
that does not already hold one.
Syntax
$jsonArray[Key;...]
-
Key: one or more keys forming the path to the key to mark as an array.
What happens
$jsonParseloads the JSON object.-
$jsonArrayconverts the value at the given key into an empty array[]. -
You can then use
$jsonArrayAppendor other array functions to populate it.
Example 1: Convert a key to an array
$nomention
$disableInnerSpaceRemoval
$jsonParse[{"games":""}]
$jsonArray[games]
$jsonPretty[2]
What happens:
-
$jsonParseloads an object wheregamesholds an empty string. -
$jsonArray[games]convertsgamesinto an empty array. $jsonPretty[2]shows the result.
Output:
{
"games": []
}
Example 2: Create an array then append to it
$nomention
$jsonParse[{}]
$jsonArray[items]
$jsonArrayAppend[items;sword]
$jsonArrayAppend[items;shield]
$jsonJoinArray[items;, ]
What happens:
$jsonParsecreates an empty object.-
$jsonArray[items]createsitemsas an empty array. - Two
$jsonArrayAppendcalls add values to it. $jsonJoinArraydisplays the result.
Output:
sword, shield
Common uses
- Initialising an array key: create an empty array at a key before appending to it
- Resetting an array: convert a key back to an empty array to clear its contents
See also
- $jsonArrayAppend: to add values to the array after creating it
- $jsonArrayCount: to count how many elements are in the array
- $jsonParse: must be called first to load the JSON object