Back to Home
$jsonArrayCount
$jsonArrayCount returns the number of elements in a JSON
array at the specified key.
Syntax
$jsonArrayCount[Key;...]
-
Key: one or more keys forming the path to the array.
What happens
$jsonParseloads the JSON object.-
$jsonArrayCountcounts the elements in the array at the given key path and returns the number.
Example 1: Count elements in a simple array
$nomention
$jsonParse[{"fruits":["apple","banana","orange"\]}]
$jsonArrayCount[fruits]
What happens:
-
$jsonParseloads the object with afruitsarray of 3 elements. $jsonArrayCount[fruits]returns the count.
Output:
3
Example 2: Count elements in a nested array
$nomention
$jsonParse[{"store":{"items":["sword","shield","potion","arrow"\]}}]
Items in store: $jsonArrayCount[store;items]
What happens:
$jsonParseloads the object.-
$jsonArrayCount[store;items]navigates intostoreand counts theitemsarray.
Output:
Items in store: 4
Common uses
- Validation: check the size of an array before appending or accessing by index
- Displaying list size: show how many entries a list has
- Dynamic access: use the count to reach the last element without hardcoding an index
See also
- $jsonArrayIndex: to find the position of a specific value
- $jsonArrayAppend: to add elements to the array
- $jsonParse: must be called first to load the JSON object