Skip to main content

$number

$number(arg?) -> number
Compact type signature<(nsb)-:n>

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

Source: JSONata

Documentation

Casts the arg parameter to a number using the following casting rules

  • Numbers are unchanged
  • Strings that contain a sequence of characters that represent a legal JSON number are converted to that number
  • Hexadecimal numbers start with 0x, Octal numbers with 0o, binary numbers with 0b
  • Boolean true casts to 1, Boolean false casts to 0
  • All other values cause an error to be thrown.

Examples

Basic usage

  • $number("5") => 5
  • $number("0x12") => 0x18
  • ["1", "2", "3", "4", "5"].$number() => [1, 2, 3, 4, 5]

Convert a numeric string to a number

Input

This example uses the claims-lines example input. The expression reads the string-valued units field from the second claim line and converts it to a number.

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

$number($[1].units)

Result

1