$single
$single(array, function?) -> valueCompact type signature
<af?>Source: JSONata
Documentation
Returns exactly one item from the input sequence.
If you provide a predicate, $single() selects the items whose predicate result is truthy under JSONata boolean rules and then enforces that exactly one match exists. Without a predicate, it enforces that the input already contains exactly one item. undefined input returns undefined.
If no item matches, FUME throws D3139. If more than one item matches, FUME throws D3138. Use $first() when you want the first match without cardinality enforcement.
Examples
Expect exactly one matching line
Input
This example uses the claims-lines example input. The expression searches for the requested service code and asserts that exactly one line matches.
Example input
JSON
[
{
"claimId": "clm-2001",
"lineNumber": 1,
"serviceCode": "PROC-010",
"serviceDisplay": "office visit",
"units": 1,
"unitPrice": 125,
"allowedAmount": 110,
"modifiers": [
"MOD-A"
]
},
{
"claimId": "clm-2001",
"lineNumber": 2,
"serviceCode": "PROC-020",
"serviceDisplay": "imaging study",
"units": "1",
"unitPrice": 350,
"allowedAmount": 300,
"modifiers": [
"MOD-B",
"MOD-C"
]
},
{
"claimId": "clm-2001",
"lineNumber": 3,
"serviceCode": "PROC-030",
"serviceDisplay": "lab panel",
"units": 2,
"unitPrice": 40,
"allowedAmount": 70,
"modifiers": []
},
{
"claimId": "clm-2002",
"lineNumber": 1,
"serviceCode": "PROC-010",
"serviceDisplay": "office visit",
"units": 1,
"unitPrice": 125,
"allowedAmount": 115,
"modifiers": [
"MOD-A"
]
},
{
"claimId": "clm-2002",
"lineNumber": 2,
"serviceCode": "PROC-040",
"serviceDisplay": "counseling",
"units": 3,
"unitPrice": 60,
"allowedAmount": 150,
"modifiers": [
"MOD-D"
]
}
]
Expression
$single($, function($l) { $l.serviceCode = "PROC-040" }).allowedAmount
Result
150