$each
$each(object?, function) -> arrayCompact type signature
<o-f:a>If object is omitted, the current context value is used.
Source: JSONata
Documentation
Applies a function to each key-value pair in an object and returns the collected results as a sequence.
The callback receives the value, key, and source object. Results that evaluate to undefined are omitted from the returned sequence, so use an explicit value such as null if you need a placeholder output. Use $map() for array-oriented iteration; $each() is the object-member variant.
Examples
Iterate over object members
Input
This example uses the patient-summary example input. The expression selects the nested identifiers object and iterates over its key-value pairs.
Example input
JSON
{
"patientId": "pat-0001",
"name": {
"given": "Avery",
"family": "Reed",
"display": "Avery Reed"
},
"birthDate": "1990-06-12",
"sex": "female",
"primaryCareTeam": {
"organization": "ExampleCare",
"facility": "ExampleCare Clinic",
"practitioner": {
"practitionerId": "prac-4001",
"display": "Jordan Kim"
}
},
"identifiers": {
"memberId": "mbr-3001",
"recordNumber": "rec-5001"
},
"tags": [
"demo",
"fictional"
],
"_xmlTagName": "patientSummary"
}
Expression
$each(identifiers, function($v, $k) { $k & "=" & $v })
Result
[
"memberId=mbr-3001",
"recordNumber=rec-5001"
]