# Chalk SQL Reference: JSON Functions
source: https://docs.chalk.ai/chalksql-docs#section-json-functions

## `cast_to_json(value)`
source: https://docs.chalk.ai/chalksql-docs#cast_to_json

Converts an arbitrary json-compatible `value` into json.

**Type parameters.** `T` means matching inputs and outputs use the same type.

**Parameters**

- `value`: `$T`
  The input `value`. `T` means matching inputs and outputs use the same type.

**Examples**

```sql
SELECT cast_to_json({'merchant': 'Blue Bottle', 'amount': 4.5, 'tags': ['coffee', 'recurring']}) AS txn_json;
```

```text
{"amount":4.5,"merchant":"Blue Bottle","tags":["coffee","recurring"]}
```

```sql
SELECT cast_to_json('{"merchant": "Blue Bottle"}') AS memo_json;
```

```text
"{\"merchant\": \"Blue Bottle\"}"
```

## `get_json_value(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#get_json_value

Alias for [json_extract](https://docs.chalk.ai/chalksql-docs#json_extract).

**Examples**

```sql
SELECT get_json_value('{"customer": {"id": 4821, "tier": "gold"}, "items": [{"sku": "A-100", "qty": 2}]}', '$.customer.tier') AS customer_tier;
```

```text
"gold"
```

```sql
SELECT get_json_value('{"order_id": "ord_771", "items": [{"sku": "A-100", "qty": 2}, {"sku": "B-205", "qty": 1}]}', '$.items[*].sku') AS order_skus;
```

```text
["A-100","B-205"]
```

```sql
SELECT CAST(get_json_value('{"txn": {"amount": 129.99, "currency": "USD"}}', '$.txn.amount') AS DOUBLE) * 100 AS amount_cents;
```

```text
12999.0
```

## `get_json_value_strict(json, json_path, ...)`
source: https://docs.chalk.ai/chalksql-docs#get_json_value_strict

Extracts a value from JSON or a JSON string using a path expression and records invalid JSON errors.

**Examples**

```sql
SELECT get_json_value_strict('{"merchant": {"name": "Blue Bottle Coffee", "mcc": "5814"}, "amount": 6.25}', '$.merchant.mcc') AS merchant_mcc;
```

```text
"5814"
```

```sql
SELECT CAST(get_json_value_strict('{"order_id": "ord_918", "items": [{"sku": "A1", "qty": 2}, {"sku": "B7", "qty": 1}], "total_cents": 4598}', '$.items[0].qty') AS BIGINT) AS first_item_qty;
```

```text
2
```

```sql
SELECT coalesce(CAST(get_json_value_strict('{"user_id": 42, "device": {"os": "ios"}}', '$.device.browser', false) AS VARCHAR), 'unknown') AS device_browser;
```

```text
unknown
```

**Overloads**

#### `get_json_value_strict(json, json_path)`

Extracts a value from JSON using a path expression. Records errors for invalid JSON.

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

#### `get_json_value_strict(json, json_path)`

Extracts a value from JSON string using a path expression. Records errors for invalid JSON.

**Parameters**

- `json`: `large_string`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

#### `get_json_value_strict(json, json_path, error_on_missing)`

Extracts a value from JSON using a path expression. Records errors for invalid JSON.

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.
- `error_on_missing`: `bool`
  Whether a missing path raises an error instead of returning null.

#### `get_json_value_strict(json, json_path, error_on_missing)`

Extracts a value from JSON using a path expression. Records errors for invalid JSON.

**Parameters**

- `json`: `large_string`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.
- `error_on_missing`: `bool`
  Whether a missing path raises an error instead of returning null.

## `is_json_scalar(json)`
source: https://docs.chalk.ai/chalksql-docs#is_json_scalar

Returns whether a JSON value or JSON string represents a scalar.

**Examples**

```sql
SELECT transform(ARRAY['{"merchant": "Acme Grocery"}', '{"merchant": {"name": "Blue Bottle", "mcc": 5814}}'], p -> CASE WHEN is_json_scalar(json_extract(p, '$.merchant')) THEN json_extract_scalar(p, '$.merchant') ELSE json_extract_scalar(p, '$.merchant.name') END) AS merchant_name;
```

```text
[Acme Grocery, Blue Bottle]
```

```sql
SELECT transform(ARRAY['42', '"refund"', 'true', 'null', '[1,2]', '{"a":1}'], x -> is_json_scalar(x)) AS is_scalar;
```

```text
[true, true, true, true, false, false]
```

**Overloads**

#### `is_json_scalar(json)`

Returns true if the JSON value is a scalar (number, string, true, false, or null).

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.

#### `is_json_scalar(json)`

Returns true if the JSON string represents a scalar value.

**Parameters**

- `json`: `large_string`
  The input JSON value or JSON string.

## `json_array_contains(json, value)`
source: https://docs.chalk.ai/chalksql-docs#json_array_contains

Checks if a `value` exists in a JSON array.

**Examples**

```sql
SELECT json_array_contains(json_extract(json_parse('{"ticket_id": 5521, "tags": ["billing", "refund", "urgent"]}'), '$.tags'), 'urgent') AS is_urgent;
```

```text
true
```

```sql
SELECT json_array_contains(json_parse('[12, 47, 305]'), '47') AS string_matches_number;
```

```text
false
```

**Overloads**

#### `json_array_contains(json, value)`

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `value`: `bool`
  The input `value`.

#### `json_array_contains(json, value)`

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `value`: `int64`
  The input `value`.

#### `json_array_contains(json, value)`

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `value`: `double`
  The input `value`.

#### `json_array_contains(json, value)`

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `value`: `large_string`
  The input `value`.

## `json_array_length(json)`
source: https://docs.chalk.ai/chalksql-docs#json_array_length

Returns the number of elements in a JSON array.

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.

**Examples**

```sql
SELECT json_array_length(json_extract('{"order_id":"ord_918","items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1},{"sku":"C3","qty":4}]}', '$.items')) AS line_item_count;
```

```text
3
```

```sql
SELECT coalesce(json_array_length(json_extract('{"user_id":42}', '$.prior_chargebacks')), 0) AS prior_chargeback_count;
```

```text
0
```

## `json_extract(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#json_extract

Extracts a value from JSON or a JSON string using a path expression.

**Examples**

```sql
SELECT json_extract('{"order_id": "A-1042", "items": [{"sku": "MUG-01", "qty": 2}, {"sku": "TEE-XL", "qty": 1}]}', '$.items[*].sku') AS skus;
```

```text
["MUG-01","TEE-XL"]
```

```sql
SELECT CAST(json_extract('{"device": {"os": "iOS", "risk_score": 0.82}}', '$.device.risk_score') AS DOUBLE) > 0.7 AS high_risk_device;
```

```text
true
```

**Overloads**

#### `json_extract(json, json_path)`

Extracts a value from JSON using a path expression.

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

#### `json_extract(json, json_path)`

Extracts a value from JSON string using a path expression.

**Parameters**

- `json`: `large_string`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

## `json_extract_array(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#json_extract_array

Extracts an array from JSON string using a path expression.

**Examples**

```sql
SELECT cardinality(json_extract_array('{"txn_id":"t-77","risk_flags":["new_device","vpn","velocity"]}', '$.risk_flags')) AS risk_flag_count;
```

```text
3
```

```sql
SELECT json_extract_array('{"order_id":"o-981","items":[{"sku":"SKU-1042","qty":2},{"sku":"SKU-2210","qty":1},{"sku":"SKU-1042","qty":1}]}', '$.items[*].sku') AS skus;
```

```text
["SKU-1042", "SKU-2210", "SKU-1042"]
```

**Overloads**

#### `json_extract_array(json, json_path)`

**Parameters**

- `json`: `large_string`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

#### `json_extract_array(json, json_path)`

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

## `json_extract_path(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#json_extract_path

Alias for [json_extract](https://docs.chalk.ai/chalksql-docs#json_extract).

**Examples**

```sql
SELECT json_extract_path('{"event":"charge.succeeded","data":{"amount":4999,"card":{"brand":"visa","last4":"4242","country":"US"}}}', '$.data.card') AS card_details;
```

```text
{"brand":"visa","country":"US","last4":"4242"}
```

```sql
SELECT json_extract_path('{"order_id":"A-1001","items":[{"sku":"MUG-01","qty":2},{"sku":"TEE-M","qty":1}]}', '$.items[1].sku') AS second_item_sku;
```

```text
"TEE-M"
```

## `json_extract_path_text(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#json_extract_path_text

Alias for [json_extract_scalar](https://docs.chalk.ai/chalksql-docs#json_extract_scalar).

**Examples**

```sql
SELECT json_extract_path_text('{"merchant": {"name": "Blue Bottle Coffee", "mcc": "5814"}, "amount": 6.75}', '$.merchant.mcc') AS merchant_mcc;
```

```text
5814
```

```sql
SELECT json_extract_path_text('{"order_id": "ord_8812", "items": [{"sku": "A-100", "qty": 1}, {"sku": "B-200", "qty": 3}]}', '$.items[1].sku') AS second_item_sku;
```

```text
B-200
```

```sql
SELECT CAST(json_extract_path_text('{"merchant": {"name": "Blue Bottle Coffee"}, "amount": 1250.00}', '$.amount') AS DOUBLE) > 1000 AS is_large_txn;
```

```text
true
```

## `json_extract_scalar(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#json_extract_scalar

Extracts a scalar value from JSON or a JSON string using a JSONPath expression.

**Examples**

```sql
SELECT CAST(json_extract_scalar('{"event": "charge.succeeded", "data": {"amount": 4999, "currency": "usd", "card": {"country": "CA"}}}', '$.data.amount') AS DOUBLE) / 100 AS amount_dollars;
```

```text
49.99
```

```sql
SELECT json_extract_scalar('{"order_id": "o-881", "items": [{"sku": "SKU-42", "qty": 2}, {"sku": "SKU-7", "qty": 1}]}', '$.items[0].sku') AS first_sku;
```

```text
SKU-42
```

```sql
SELECT json_extract_scalar('{"items": [{"sku": "SKU-42"}]}', '$.items') IS NULL AS array_path_is_null;
```

```text
true
```

**Overloads**

#### `json_extract_scalar(json, json_path)`

Extracts a scalar value from JSON using a JSONPath expression.

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

#### `json_extract_scalar(json, json_path)`

Extracts a scalar value from a JSON string using a JSONPath expression.

**Parameters**

- `json`: `large_string`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

## `json_extract_string(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#json_extract_string

Alias for [json_extract_scalar](https://docs.chalk.ai/chalksql-docs#json_extract_scalar).

**Examples**

```sql
SELECT json_extract_string('{"event": "charge.failed", "data": {"card": {"brand": "visa", "decline_code": "insufficient_funds"}}}', '$.data.card.decline_code') AS decline_code;
```

```text
insufficient_funds
```

```sql
SELECT json_extract_string('{"items": [{"sku": "SHOE-42", "qty": 1}, {"sku": "SOCK-07", "qty": 3}]}', '$.items[1].sku') AS second_sku;
```

```text
SOCK-07
```

```sql
SELECT coalesce(json_extract_string('{"device": {"os": "ios"}}', '$.device.browser'), 'unknown') AS browser;
```

```text
unknown
```

## `json_format(json)`
source: https://docs.chalk.ai/chalksql-docs#json_format

Encodes a JSON value into a string

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.

**Examples**

```sql
SELECT json_format(json_extract(json_parse('{"order_id": "A-1009", "shipping": {"country": "US", "zip": "94107"}}'), '$.shipping')) AS shipping_json;
```

```text
{"country":"US","zip":"94107"}
```

```sql
SELECT json_format(json_parse('{ "merchant": "Blue Bottle",  "amount": 4.50, "currency": "USD" }')) AS canonical_payload;
```

```text
{"amount":4.50,"currency":"USD","merchant":"Blue Bottle"}
```

```sql
SELECT json_format(cast_to_json(MAP(ARRAY['failed_logins_1h', 'distinct_ips_24h'], ARRAY[3, 2]))) AS risk_signals_json;
```

```text
{"distinct_ips_24h":2,"failed_logins_1h":3}
```

## `json_is_null(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#json_is_null

Returns true when a JSON path resolves to a JSON null value.

**Examples**

```sql
SELECT transform(ARRAY['{"amount": 12.5, "merchant_id": null}', '{"amount": 40.0}', '{"amount": 9.99, "merchant_id": "m_881"}'], p -> json_is_null(p, '$.merchant_id')) AS merchant_explicitly_null;
```

```text
[true, false, false]
```

```sql
SELECT transform(ARRAY['{"kyc": {"ssn_last4": null}}', '{"kyc": {}}', '{"kyc": {"ssn_last4": "1234"}}'], p -> CASE WHEN json_is_null(p, '$.kyc.ssn_last4') THEN 'declined' WHEN json_extract_scalar(p, '$.kyc.ssn_last4') IS NULL THEN 'not_asked' ELSE 'provided' END) AS ssn_status;
```

```text
[declined, not_asked, provided]
```

**Overloads**

#### `json_is_null(json, json_path)`

**Parameters**

- `json`: `large_string`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

#### `json_is_null(json, json_path)`

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `json_path`: `large_string`
  The JSON path expression, such as $.a.b.

## `json_parse(string)`
source: https://docs.chalk.ai/chalksql-docs#json_parse

Converts an arbitrary value into a JSON `string`

**Parameters**

- `string`: `large_string`
  The input `string`.

**Examples**

```sql
SELECT json_parse('{"user_id": 42,  "plan": "pro" , "seats": 5}') AS profile;
```

```text
{"plan":"pro","seats":5,"user_id":42}
```

```sql
SELECT json_extract_scalar(json_parse('{"event": "login", "device": {"os": "ios", "version": "17.2"}}'), '$.device.os') AS device_os;
```

```text
ios
```

## `json_size(json, value)`
source: https://docs.chalk.ai/chalksql-docs#json_size

Returns the size of a JSON object or array at a path.

**Examples**

```sql
SELECT json_size('{"order_id": "A-1042", "items": [{"sku": "TEE-M"}, {"sku": "MUG-01"}, {"sku": "CAP-L"}]}', '$.items') AS line_item_count;
```

```text
3
```

```sql
SELECT json_size('{"device": {"os": "iOS", "model": "iPhone15", "locale": "en-US", "vpn": false}, "ip": "203.0.113.9"}', '$.device') AS device_attr_count;
```

```text
4
```

**Overloads**

#### `json_size(json, value)`

Returns the size of a JSON object or array at the given path, or 0 for scalar values.

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.
- `value`: `large_string`
  The input `value`.

#### `json_size(json, value)`

Returns the size of a JSON `value` at the given path.

**Parameters**

- `json`: `large_string`
  The input JSON value or JSON string.
- `value`: `large_string`
  The input `value`.

## `json_stringify(json)`
source: https://docs.chalk.ai/chalksql-docs#json_stringify

Encodes a JSON value into a string. This is an alias for `json_format`.

**Parameters**

- `json`: `extension<arrow.json>`
  The input JSON value or JSON string.

**Examples**

```sql
SELECT json_stringify(json_parse('{"merchant": "Blue Bottle", "amount": 4.5, "tags": ["coffee", "food"]}')) AS payload;
```

```text
{"amount":4.5,"merchant":"Blue Bottle","tags":["coffee","food"]}
```

```sql
SELECT json_stringify(json_extract(json_parse('{"user": {"id": 42, "tier": "gold"}, "event": "login"}'), '$.user')) AS user_json;
```

```text
{"id":42,"tier":"gold"}
```

## `json_value(json, json_path)`
source: https://docs.chalk.ai/chalksql-docs#json_value

Alias for [json_extract_scalar](https://docs.chalk.ai/chalksql-docs#json_extract_scalar).

**Examples**

```sql
SELECT json_value('{"merchant": {"name": "Blue Bottle Coffee", "mcc": "5814"}, "amount": 6.75}', '$.merchant.mcc') AS merchant_mcc;
```

```text
5814
```

```sql
SELECT CAST(json_value('{"order_id": "A-1042", "items": [{"sku": "TSHIRT-M", "qty": 2, "unit_price": 18.50}, {"sku": "MUG-01", "qty": 1, "unit_price": 12.00}]}', '$.items[0].unit_price') AS DOUBLE) * 2 AS first_line_total;
```

```text
37.0
```

## `jsonify(value)`
source: https://docs.chalk.ai/chalksql-docs#jsonify

Converts an arbitrary `value` into a JSON string

**Type parameters.** `T` means matching inputs and outputs use the same type.

**Parameters**

- `value`: `$T`
  The input `value`. `T` means matching inputs and outputs use the same type.

**Examples**

```sql
SELECT jsonify({'txn_id': 'txn_8841', 'amount': 42.5, 'tags': ['grocery', 'card_present']}) AS txn_json;
```

```text
{"amount":42.5,"tags":["grocery","card_present"],"txn_id":"txn_8841"}
```

```sql
SELECT jsonify({'user_id': 1017, 'risk_score': 0.82, 'is_flagged': true, 'last_login': NULL, 'shipping': {'country': 'US', 'zip': '94107'}}) AS user_json;
```

```text
{"is_flagged":true,"last_login":null,"risk_score":0.82,"shipping":{"country":"US","zip":"94107"},"user_id":1017}
```

## `regorus_eval(policy, query, ...+2)`
source: https://docs.chalk.ai/chalksql-docs#regorus_eval

Evaluate a Rego `query` against a `policy` via the regorus interpreter. Arguments are (`policy`, `query`, `input_json`, `data_json`); the result is the regorus QueryResults serialized as a JSON string.

**Examples**

```sql
SELECT json_extract_scalar(regorus_eval('package txn
default allow := false
allow if { input.amount < 1000; input.country == "US" }', 'data.txn.allow', '{"amount": 250, "country": "US"}', ''), '$.result[0].expressions[0].value') AS allow;
```

```text
true
```

```sql
SELECT json_extract(regorus_eval('package risk
reasons contains "amount_over_limit" if input.amount > data.limits.max_amount
reasons contains "blocked_country" if input.country in data.limits.blocked_countries
reasons contains "new_account" if input.account_age_days < 30', 'data.risk.reasons', '{"amount": 4200, "country": "KP", "account_age_days": 400}', '{"limits": {"max_amount": 2500, "blocked_countries": ["KP", "IR"]}}'), '$.result[0].expressions[0].value') AS flag_reasons;
```

```text
["amount_over_limit","blocked_country"]
```

**Overloads**

#### `regorus_eval(policy, query, ...+2)`

**Parameters**

- `policy`: `string`
  The Rego `policy` source.
- `query`: `string`
  The `query` to evaluate.
- `input_json`: `string`
  The input document as a JSON string.
- `data_json`: `string`
  The data document as a JSON string.

#### `regorus_eval(policy, query, ...+2)`

**Parameters**

- `policy`: `large_string`
  The Rego `policy` source.
- `query`: `large_string`
  The `query` to evaluate.
- `input_json`: `large_string`
  The input document as a JSON string.
- `data_json`: `large_string`
  The data document as a JSON string.

## `json ->> json_path`
source: https://docs.chalk.ai/chalksql-docs#-%3E%3E

Alias for [json_extract_scalar](https://docs.chalk.ai/chalksql-docs#json_extract_scalar).

**Examples**

```sql
SELECT '{"family": "anatidae"}' ->> '$.family';
```
