Skip to main content
Read, test, and reshape JSON values such as the user_properties column on users and the properties column on events, plus ClickHouse Map values extracted from them.

->> (get text)

Extracts a value from a JSON object by key (or from a JSON array by index) and returns it as text, which is what you usually want for filtering or display. Syntax
Arguments
  • json — the JSON value to read from.
  • key — the object key (text) or array index (integer) to extract.
Returned value
  • The extracted value as text, or NULL if the key or index is absent. Text.
Example
Users on the pro plan

-> (get JSON)

Extracts a value from a JSON object by key (or from a JSON array by index), returning it as a JSON value so it can be chained for nested lookups. Syntax
Arguments
  • json — the JSON value to read from.
  • key — the object key (text) or array index (integer) to extract.
Returned value
  • The extracted value as JSON, or NULL if the key or index is absent. JSON.
Example
Country from a nested address object

@> (contains)

Tests whether the left JSON value contains the right JSON value — every key/value pair (or array element) on the right must be present on the left. Syntax
Arguments
  • json — the JSON value to search within.
  • subset — a JSON value that must be contained; supply it as a JSON-formatted string literal.
Returned value
  • true if json contains subset, otherwise false. Boolean.
Example
Users whose properties include plan pro
An explicit ::jsonb cast on either side — user_properties::jsonb @> '{"plan": "pro"}'::jsonb — behaves identically; the redundant cast is stripped.

= ANY (JSON array)

Tests whether a scalar value equals any element of a JSON array. It is the common way to check membership in a JSON array stored on a property. Syntax
Arguments
  • value — the scalar to look for.
  • json_array — a JSON array value, such as user_properties -> 'tags'.
Returned value
  • true if value matches at least one element, otherwise false. Boolean.
Example
Users tagged vip

@? (path exists)

Tests whether a JSON path exists within a JSON value. Syntax
Arguments
  • json — the JSON value to test.
  • path — a JSON path string, such as '$.plan'.
Returned value
  • true if the path matches something in json, otherwise false. Boolean.
Example
Users who have a plan property set

jsonExtractInt

Extracts the value at a key from a JSON value as a signed integer. Syntax
Arguments
  • json — the JSON value to read from.
  • key — the object key to extract.
Returned value
  • The value as a signed integer; 0 if the key is absent or not numeric. Integer.
Example
Users with a negative account balance

jsonExtractFloat

Extracts the value at a key from a JSON value as a floating-point number. Syntax
Arguments
  • json — the JSON value to read from.
  • key — the object key to extract.
Returned value
  • The value as a float; 0 if the key is absent or not numeric. Float.
Example
Users with a lifetime value above 500

jsonExtractBool

Extracts the value at a key from a JSON value as a boolean. Syntax
Arguments
  • json — the JSON value to read from.
  • key — the object key to extract.
Returned value
  • The value as a boolean; false if the key is absent or not a boolean. Boolean.
Example
Users flagged as verified

jsonExtractUInt

Extracts the value at a key from a JSON value as an unsigned integer. Syntax
Arguments
  • json — the JSON value to read from.
  • key — the object key to extract.
Returned value
  • The value as an unsigned integer; 0 if the key is absent or not numeric. Unsigned integer.
Example
Users with at least ten logins

is_json_valid

Tests whether a string is well-formed JSON. Syntax
Arguments
  • text — the string to validate.
Returned value
  • true if the string parses as valid JSON, otherwise false. Boolean.
Example
Events with a well-formed properties payload

is_json_object

Tests whether a JSON value is an object (rather than an array, string, number, or scalar). Syntax
Arguments
  • json — the JSON value to inspect.
Returned value
  • true if the value is a JSON object, otherwise false. Boolean.
Example
Rows where the address property is an object

json_path_exists

Tests whether a JSON path exists within a JSON value. It is the function form of the @? operator. Syntax
Arguments
  • json — the JSON value to test.
  • path — a JSON path string, such as '$.address.country'.
Returned value
  • true if the path matches something in json, otherwise false. Boolean.
Example
Users with a country in their address

json_has_any

Tests whether a JSON object contains any of the given top-level keys. Syntax
Arguments
  • json — the JSON object to inspect.
  • keys — an array of key names to look for.
Returned value
  • true if at least one of keys is present as a top-level key, otherwise false. Boolean.
Example
Users with a plan or tier property

jsonb_array_length

Returns the number of elements in a JSON array. Syntax
Arguments
  • json_array — the JSON array to measure.
Returned value
  • The element count. Integer.
Example
Users with more than three tags

jsonb_object_keys

Expands the top-level keys of a JSON object into a set of rows, one key per row. Syntax
Arguments
  • json — the JSON object whose keys to list.
Returned value
  • A set of rows, each carrying one top-level key as text. Set of text values.
Example
One row per property key per user

jsonb_array_elements_text

Expands a JSON array into a set of rows, one per element, with each element returned as text. Syntax
Arguments
  • json_array — the JSON array to expand.
Returned value
  • A set of rows, each carrying one array element as text. Set of text values.
Example
One row per tag per user

jsonb_build_object

Builds a JSON object from alternating key and value arguments. Syntax
Arguments
  • key1, key2, ... — object keys (text).
  • value1, value2, ... — the value paired with each preceding key.
Returned value
  • A JSON object built from the key/value pairs. JSON.
Example
Repackage plan and country into an object

jsonb_build_array

Builds a JSON array from the given arguments, in order. Syntax
Arguments
  • value1, value2, ... — the values to place into the array, in order.
Returned value
  • A JSON array containing the arguments. JSON.
Example
A JSON array of a user's plan and country

::jsonb

Casts an expression to the jsonb type. It is most often used to give a string literal an explicit JSON type in a containment test. Syntax
Arguments
  • expression — the value to cast, typically a JSON-formatted string literal or a JSON column.
Returned value
  • The value as jsonb. JSON.
Example
Cast a literal for a containment test
On ClickHouse the ::jsonb cast is a no-op on a String-JSON column and resolves to the same path as a bare ->>.

mapContains

Tests whether a map contains a given key. Syntax
Arguments
  • map — the map to inspect.
  • key — the key to look for.
Returned value
  • 1 if the map contains key, otherwise 0.
Example
Users whose metrics map tracks logins

mapKeys

Returns the keys of a map as an array (ClickHouse only). Syntax
Arguments
  • map — the map to read.
Returned value
  • An array of the map’s keys. Array.
Example
The set of metric names per user

mapValues

Returns the values of a map as an array (ClickHouse only). Syntax
Arguments
  • map — the map to read.
Returned value
  • An array of the map’s values. Array.
Example
All metric values per user

mapAdd

Merges two maps, summing the values of any keys they share. Keys present in only one map are carried through unchanged. Both maps must have numeric values (ClickHouse only). Syntax
Arguments
  • map1 — the first map with numeric values.
  • map2 — the second map with numeric values.
Returned value
  • A map whose values are the per-key sums. Map.
Example
Combine two quarterly usage maps

mapSubtract

Merges two maps, subtracting the second map’s values from the first for any keys they share. Both maps must have numeric values (ClickHouse only). Syntax
Arguments
  • map1 — the map to subtract from.
  • map2 — the map of values to subtract.
Returned value
  • A map whose values are the per-key differences. Map.
Example
Change in usage from Q1 to Q2

mapPopulateSeries

Fills the gaps in a map with integer keys, inserting missing consecutive keys with a zero value up to a given maximum. It is useful for turning a sparse counter map into a dense one (ClickHouse only). Syntax
Arguments
  • map — a map with integer keys and numeric values.
  • max — optional highest key to fill up to; defaults to the map’s largest key.
Returned value
  • A map with every integer key from the smallest key up to max, gaps filled with zero. Map.
Example
Dense day-of-week login counts up to day 7

jsonb_strip_nulls

Returns a copy of a JSON value with every object field whose value is JSON null removed. Null array elements are left untouched (PostgreSQL only). Syntax
Arguments
  • json — the JSON value to clean.
Returned value
  • The JSON value with null-valued object fields removed. JSON.
Example
Drop null-valued properties