Converts an arbitrary json-compatible value into json.

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

Parameters
The input value. T means matching inputs and outputs use the same type.
Examples
SELECT cast_to_json({'merchant': 'Blue Bottle', 'amount': 4.5, 'tags': ['coffee', 'recurring']}) AS txn_json;
| txn_json                                                              |
| --------------------------------------------------------------------- |
| {"amount":4.5,"merchant":"Blue Bottle","tags":["coffee","recurring"]} |
SELECT cast_to_json('{"merchant": "Blue Bottle"}') AS memo_json;
| memo_json                             |
| ------------------------------------- |
| "{\\"merchant\\": \\"Blue Bottle\\"}" |
Alias for json_extract.
Examples
SELECT get_json_value('{"customer": {"id": 4821, "tier": "gold"}, "items": [{"sku": "A-100", "qty": 2}]}', '$.customer.tier') AS customer_tier;
| customer_tier |
| ------------- |
| "gold"        |
SELECT get_json_value('{"order_id": "ord_771", "items": [{"sku": "A-100", "qty": 2}, {"sku": "B-205", "qty": 1}]}', '$.items[*].sku') AS order_skus;
| order_skus        |
| ----------------- |
| ["A-100","B-205"] |
SELECT CAST(get_json_value('{"txn": {"amount": 129.99, "currency": "USD"}}', '$.txn.amount') AS DOUBLE) * 100 AS amount_cents;
| amount_cents |
| ------------ |
| 12999.0      |
Extracts a value from JSON or a JSON string using a path expression and records invalid JSON errors.
Examples
SELECT get_json_value_strict('{"merchant": {"name": "Blue Bottle Coffee", "mcc": "5814"}, "amount": 6.25}', '$.merchant.mcc') AS merchant_mcc;
| merchant_mcc |
| ------------ |
| "5814"       |
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;
| first_item_qty |
| -------------- |
| 2              |
SELECT coalesce(CAST(get_json_value_strict('{"user_id": 42, "device": {"os": "ios"}}', '$.device.browser', false) AS VARCHAR), 'unknown') AS device_browser;
| device_browser |
| -------------- |
| unknown        |
Returns whether a JSON value or JSON string represents a scalar.
Examples
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;
| merchant_name               |
| --------------------------- |
| [Acme Grocery, Blue Bottle] |
SELECT transform(ARRAY['42', '"refund"', 'true', 'null', '[1,2]', '{"a":1}'], x -> is_json_scalar(x)) AS is_scalar;
| is_scalar                              |
| -------------------------------------- |
| [true, true, true, true, false, false] |
Checks if a value exists in a JSON array.
Examples
SELECT json_array_contains(json_extract(json_parse('{"ticket_id": 5521, "tags": ["billing", "refund", "urgent"]}'), '$.tags'), 'urgent') AS is_urgent;
| is_urgent |
| --------- |
| true      |
SELECT json_array_contains(json_parse('[12, 47, 305]'), '47') AS string_matches_number;
| string_matches_number |
| --------------------- |
| false                 |
Returns the number of elements in a JSON array.
Parameters
json:
extension<arrow.json>
The input JSON value or JSON string.
Examples
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;
| line_item_count |
| --------------- |
| 3               |
SELECT coalesce(json_array_length(json_extract('{"user_id":42}', '$.prior_chargebacks')), 0) AS prior_chargeback_count;
| prior_chargeback_count |
| ---------------------- |
| 0                      |
Extracts a value from JSON or a JSON string using a path expression.
Examples
SELECT json_extract('{"order_id": "A-1042", "items": [{"sku": "MUG-01", "qty": 2}, {"sku": "TEE-XL", "qty": 1}]}', '$.items[*].sku') AS skus;
| skus                |
| ------------------- |
| ["MUG-01","TEE-XL"] |
SELECT CAST(json_extract('{"device": {"os": "iOS", "risk_score": 0.82}}', '$.device.risk_score') AS DOUBLE) > 0.7 AS high_risk_device;
| high_risk_device |
| ---------------- |
| true             |
Extracts an array from JSON string using a path expression.
Examples
SELECT cardinality(json_extract_array('{"txn_id":"t-77","risk_flags":["new_device","vpn","velocity"]}', '$.risk_flags')) AS risk_flag_count;
| risk_flag_count |
| --------------- |
| 3               |
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;
| skus                                 |
| ------------------------------------ |
| ["SKU-1042", "SKU-2210", "SKU-1042"] |
Alias for json_extract.
Examples
SELECT json_extract_path('{"event":"charge.succeeded","data":{"amount":4999,"card":{"brand":"visa","last4":"4242","country":"US"}}}', '$.data.card') AS card_details;
| card_details                                   |
| ---------------------------------------------- |
| {"brand":"visa","country":"US","last4":"4242"} |
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;
| second_item_sku |
| --------------- |
| "TEE-M"         |
Examples
SELECT json_extract_path_text('{"merchant": {"name": "Blue Bottle Coffee", "mcc": "5814"}, "amount": 6.75}', '$.merchant.mcc') AS merchant_mcc;
| merchant_mcc |
| ------------ |
| 5814         |
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;
| second_item_sku |
| --------------- |
| B-200           |
SELECT CAST(json_extract_path_text('{"merchant": {"name": "Blue Bottle Coffee"}, "amount": 1250.00}', '$.amount') AS DOUBLE) > 1000 AS is_large_txn;
| is_large_txn |
| ------------ |
| true         |
Extracts a scalar value from JSON or a JSON string using a JSONPath expression.
Examples
SELECT CAST(json_extract_scalar('{"event": "charge.succeeded", "data": {"amount": 4999, "currency": "usd", "card": {"country": "CA"}}}', '$.data.amount') AS DOUBLE) / 100 AS amount_dollars;
| amount_dollars |
| -------------- |
| 49.99          |
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;
| first_sku |
| --------- |
| SKU-42    |
SELECT json_extract_scalar('{"items": [{"sku": "SKU-42"}]}', '$.items') IS NULL AS array_path_is_null;
| array_path_is_null |
| ------------------ |
| true               |
Examples
SELECT json_extract_string('{"event": "charge.failed", "data": {"card": {"brand": "visa", "decline_code": "insufficient_funds"}}}', '$.data.card.decline_code') AS decline_code;
| decline_code       |
| ------------------ |
| insufficient_funds |
SELECT json_extract_string('{"items": [{"sku": "SHOE-42", "qty": 1}, {"sku": "SOCK-07", "qty": 3}]}', '$.items[1].sku') AS second_sku;
| second_sku |
| ---------- |
| SOCK-07    |
SELECT coalesce(json_extract_string('{"device": {"os": "ios"}}', '$.device.browser'), 'unknown') AS browser;
| browser |
| ------- |
| unknown |
Encodes a JSON value into a string
Parameters
json:
extension<arrow.json>
The input JSON value or JSON string.
Examples
SELECT json_format(json_extract(json_parse('{"order_id": "A-1009", "shipping": {"country": "US", "zip": "94107"}}'), '$.shipping')) AS shipping_json;
| shipping_json                  |
| ------------------------------ |
| {"country":"US","zip":"94107"} |
SELECT json_format(json_parse('{ "merchant": "Blue Bottle",  "amount": 4.50, "currency": "USD" }')) AS canonical_payload;
| canonical_payload                                         |
| --------------------------------------------------------- |
| {"amount":4.50,"currency":"USD","merchant":"Blue Bottle"} |
SELECT json_format(cast_to_json(MAP(ARRAY['failed_logins_1h', 'distinct_ips_24h'], ARRAY[3, 2]))) AS risk_signals_json;
| risk_signals_json                           |
| ------------------------------------------- |
| {"distinct_ips_24h":2,"failed_logins_1h":3} |
Returns true when a JSON path resolves to a JSON null value.
Examples
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;
| merchant_explicitly_null |
| ------------------------ |
| [true, false, false]     |
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;
| ssn_status                      |
| ------------------------------- |
| [declined, not_asked, provided] |
Converts an arbitrary value into a JSON string
Parameters
string:
large_string
The input string.
Examples
SELECT json_parse('{"user_id": 42,  "plan": "pro" , "seats": 5}') AS profile;
| profile                               |
| ------------------------------------- |
| {"plan":"pro","seats":5,"user_id":42} |
SELECT json_extract_scalar(json_parse('{"event": "login", "device": {"os": "ios", "version": "17.2"}}'), '$.device.os') AS device_os;
| device_os |
| --------- |
| ios       |
Returns the size of a JSON object or array at a path.
Examples
SELECT json_size('{"order_id": "A-1042", "items": [{"sku": "TEE-M"}, {"sku": "MUG-01"}, {"sku": "CAP-L"}]}', '$.items') AS line_item_count;
| line_item_count |
| --------------- |
| 3               |
SELECT json_size('{"device": {"os": "iOS", "model": "iPhone15", "locale": "en-US", "vpn": false}, "ip": "203.0.113.9"}', '$.device') AS device_attr_count;
| device_attr_count |
| ----------------- |
| 4                 |
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
SELECT json_stringify(json_parse('{"merchant": "Blue Bottle", "amount": 4.5, "tags": ["coffee", "food"]}')) AS payload;
| payload                                                          |
| ---------------------------------------------------------------- |
| {"amount":4.5,"merchant":"Blue Bottle","tags":["coffee","food"]} |
SELECT json_stringify(json_extract(json_parse('{"user": {"id": 42, "tier": "gold"}, "event": "login"}'), '$.user')) AS user_json;
| user_json               |
| ----------------------- |
| {"id":42,"tier":"gold"} |
Examples
SELECT json_value('{"merchant": {"name": "Blue Bottle Coffee", "mcc": "5814"}, "amount": 6.75}', '$.merchant.mcc') AS merchant_mcc;
| merchant_mcc |
| ------------ |
| 5814         |
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;
| first_line_total |
| ---------------- |
| 37.0             |
Converts an arbitrary value into a JSON string

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

Parameters
The input value. T means matching inputs and outputs use the same type.
Examples
SELECT jsonify({'txn_id': 'txn_8841', 'amount': 42.5, 'tags': ['grocery', 'card_present']}) AS txn_json;
| txn_json                                                              |
| --------------------------------------------------------------------- |
| {"amount":42.5,"tags":["grocery","card_present"],"txn_id":"txn_8841"} |
SELECT jsonify({'user_id': 1017, 'risk_score': 0.82, 'is_flagged': true, 'last_login': NULL, 'shipping': {'country': 'US', 'zip': '94107'}}) AS user_json;
| user_json                                                                                                        |
| ---------------------------------------------------------------------------------------------------------------- |
| {"is_flagged":true,"last_login":null,"risk_score":0.82,"shipping":{"country":"US","zip":"94107"},"user_id":1017} |
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
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;
| allow |
| ----- |
| true  |
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;
| flag_reasons                            |
| --------------------------------------- |
| ["amount_over_limit","blocked_country"] |
Examples
SELECT '{"family": "anatidae"}' ->> '$.family';