Skip to main content

$spread

$spread(value?) -> value
Compact type signature<x-:a<o>>

If value is omitted, the current context value is used.

Source: JSONata

Documentation

Turns an object into an array of single-key objects.

If the input is an array, $spread() recursively spreads each item and concatenates the results. Non-object scalar values pass through unchanged, so $spread("Hello World") returns the original string rather than wrapping it in an array.

This is useful when you need to transform object members as a sequence before merging or re-projecting them.

Examples

Turn an object into an array of single-key objects

Input

This example uses the patient-summary example input. The expression selects the nested identifiers object and spreads it into single-key objects.

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

$spread(identifiers)

Result

[
{ "memberId": "mbr-3001" },
{ "recordNumber": "rec-5001" }
]