$count
$count(array) -> numberCompact type signature
<a:n>Source: JSONata
Documentation
Returns the number of items in the array parameter. If the array parameter is not an array, but rather a value of another JSON type, then the parameter is treated as a singleton array containing that value, and this function returns 1.
Examples
Basic usage
$count([1,2,3,1])=>4$count("hello")=>1
Count matching items in an array
Input
This example uses the encounters example input in both JSON and CSV because the same expression works in either format.
Example input
- JSON
- CSV
[
{
"encounterId": "enc-1001",
"patientId": "pat-0001",
"status": "finished",
"class": "outpatient",
"type": "primary-care",
"reason": "routine follow-up",
"facility": "ExampleCare Clinic",
"practitionerId": "prac-4001",
"start": "2026-02-14T09:30:00Z",
"end": "2026-02-14T10:05:00Z"
},
{
"encounterId": "enc-1002",
"patientId": "pat-0001",
"status": "finished",
"class": "outpatient",
"type": "imaging",
"reason": "headache evaluation",
"facility": "ExampleCare Imaging",
"practitionerId": "prac-4002",
"start": "2026-03-01T15:10:00Z",
"end": "2026-03-01T15:45:00Z"
},
{
"encounterId": "enc-1003",
"patientId": "pat-0001",
"status": "in-progress",
"class": "virtual",
"type": "care-coordination",
"reason": "medication questions",
"facility": "ExampleCare Clinic",
"practitionerId": "prac-4003",
"start": "2026-03-10T11:20:00Z",
"end": ""
},
{
"encounterId": "enc-1004",
"patientId": "pat-0001",
"status": "planned",
"class": "outpatient",
"type": "lab",
"reason": "fasting labs",
"facility": "ExampleCare Clinic",
"practitionerId": "prac-4001",
"start": "2026-03-20T08:00:00Z",
"end": ""
}
]
encounterId,patientId,status,class,type,reason,facility,practitionerId,start,end
enc-1001,pat-0001,finished,outpatient,primary-care,routine follow-up,ExampleCare Clinic,prac-4001,2026-02-14T09:30:00Z,2026-02-14T10:05:00Z
enc-1002,pat-0001,finished,outpatient,imaging,headache evaluation,ExampleCare Imaging,prac-4002,2026-03-01T15:10:00Z,2026-03-01T15:45:00Z
enc-1003,pat-0001,in-progress,virtual,care-coordination,medication questions,ExampleCare Clinic,prac-4003,2026-03-10T11:20:00Z,
enc-1004,pat-0001,planned,outpatient,lab,fasting labs,ExampleCare Clinic,prac-4001,2026-03-20T08:00:00Z,
Expression
$count($[status="finished"])
Result
2