Skip to main content

$split

$split(string?, separator, limit?) -> array
Compact type signature<s-(sf)n?:a<s>>

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

Browse categories

Source: JSONata

Documentation

Splits the str parameter into an array of substrings. It is an error if str is not a string.

The separator parameter can either be a string or a regular expression (regex). If it is a string, it specifies the characters within str about which it should be split. If it is the empty string, str will be split into an array of single characters. If it is a regex, it splits the string around any sequence of characters that match the regex.

The optional limit parameter is a number that specifies the maximum number of substrings to include in the resultant array. Any additional substrings are discarded. If limit is not specified, then str is fully split with no limit to the size of the resultant array. It is an error if limit is not a non-negative number.

Examples

Basic usage

  • $split("so many words", " ") => [ "so", "many", "words" ]
  • $split("so many words", " ", 2) => [ "so", "many" ]
  • $split("too much, punctuation. hard; to read", /[ ,.;]+/) => ["too", "much", "punctuation", "hard", "to", "read"]

Split an identifier into prefix + numeric part

Input

This example uses the patient-summary example input. The expression reads identifiers.memberId and splits it into parts.

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

$split(identifiers.memberId, "-")

Result

["mbr", "3001"]