Skip to main content

$merge

$merge(array) -> object
Compact type signature<a<o>:o>

Source: JSONata

Documentation

Merges an array of objects into a single object containing all the key/value pairs from each of the objects in the input array. If any of the input objects contain the same key, then the returned object will contain the value of the last one in the array. It is an error if the input array contains an item that is not an object.

Examples

Merge computed fields into a base object

Input

This example uses the patient-summary example input. The expression builds a smaller object with patient and name fields, then merges in a computed patientLabel.

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

$merge([
{
"patientId": patientId,
"name": {
"given": name.given,
"family": name.family
}
},
{
"patientLabel": name.family & ", " & name.given
}
])

Result

{
"patientId": "pat-0001",
"name": {
"given": "Avery",
"family": "Reed"
},
"patientLabel": "Reed, Avery"
}