Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

SQL JSON Extraction, Path Queries and Scalar Functions

How Cloudflare SQL, analytics, D1, pipelines, and rules languages read JSON keys, nested paths, JsonPath, and what each API does not support.

This page summarizes Cloudflare documentation for reading and filtering on JSON held in columns, log fields, or rule expressions. The same idea—pull a value by key or path—shows up in several products, but syntax, nesting, and limits differ. Match your query style to the API you are using (Basin SQL, Basin Pipelines SQL, Analytics SQL API, D1, and Ruleset Engine).

Analytics SQL API: top-level JSON keys and attributes['key']

Section titled “Analytics SQL API: top-level JSON keys and attributes['key']”

Log and analytics datasets often expose unstructured fields as JSON objects. You can project the whole object or one exact top-level key.

Some datasets expose unstructured fields as JSON objects. You can select the complete object or one exact top-level key:

— Cloudflare, Data types and literals

Example from that reference:

SELECT attributes, attributes['http.method'] AS method
FROM logs.workersLogs
WHERE accountTag = '<ACCOUNT_TAG>'
AND timestamp >= NOW() - INTERVAL '1' HOUR
LIMIT 10

Analytics SQL: why http.method is one key, not a nested path

Section titled “Analytics SQL: why http.method is one key, not a nested path”

Key lookup is exact. A key such as http.method is one top-level key name, not dot-path navigation through nested objects. If the key is absent, the result is JSON null.

On backends that allow it, an exact-key lookup can be compared to string, number, or Boolean literals, used in GROUP BY, or passed to aggregates such as SUM and AVG:

SELECT attributes['http.method'] AS method, COUNT(*) AS events
FROM logs.workersLogs
WHERE accountTag = '<ACCOUNT_TAG>'
AND timestamp >= NOW() - INTERVAL '1' HOUR
AND attributes['http.method'] = 'GET'
GROUP BY attributes['http.method']

Analytics SQL: JSON operations that are not supported

Section titled “Analytics SQL: JSON operations that are not supported”

The data types page lists hard limits you should plan around before writing complex filters:

Not supported
JSON casts
Nested traversal (beyond exact top-level key names)
Ordering by JSON values
Comparisons between two JSON values
COUNT of a JSON key
Dynamic JSON lookups inside LIKE, BETWEEN, IS NULL, or CASE

Basin SQL and Basin Pipelines: json_get, typed getters, and operators

Section titled “Basin SQL and Basin Pipelines: json_get, typed getters, and operators”

Basin Pipelines documents scalar JSON functions described as similar to those in PostgreSQL. Basin SQL exposes the same family against JSON columns (for example doc).

json_get and path arguments (keys, not JsonPath strings)

Section titled “json_get and path arguments (keys, not JsonPath strings)”

json_get retrieves a value from a JSON string by a path expressed as successive keys (and supports nested traversal via multiple arguments):

Retrieves the value from a JSON string by the specified path (keys). Returns the value as its native type (string, int, etc.).

— Cloudflare, JSON functions

SELECT json_get('{"a": {"b": 2}}', 'a', 'b') FROM source;

The -> operator chains the same idea:

SELECT '{"a": {"b": 2}}'->'a'->'b' FROM source;

json_get returns a union type; Basin SQL recommends typed variants (json_get_str, json_get_int, and others) for predictable types.

Function Role Missing or wrong-type behavior (Pipelines docs)
json_get_str String at path Empty string
json_get_int Integer at path 0
json_get_float Float at path 0.0
json_get_bool Boolean at path false
json_get_json Nested JSON as raw JSON string —
json_as_text Any value as string Also ->> in Pipelines
json_length Length of object or array at path 0 if path missing or not object/array
json_get_array JSON array as list of strings Basin SQL only

You can also cast: json_get('{"a": {"b": 2}}', 'a', 'b')::int.

To test whether a key exists:

Returns true if the JSON string contains the specified key(s).

— Cloudflare, JSON functions

SELECT json_contains('{"a": 1, "b": 2, "c": 3}', 'a') FROM source;
SELECT '{"a": 1, "b": 2, "c": 3}' ? 'a' FROM source;

Basin SQL describes json_contains as checking that a key path exists in the JSON.

JsonPath: extract_json and extract_json_string

Section titled “JsonPath: extract_json and extract_json_string”

Basin Pipelines also supports JsonPath (an evolving standard) for queries that fit path expressions better than chained keys:

Returns the JSON elements in the first argument that match the JsonPath in the second argument. The returned value is an array of json strings.

— Cloudflare, JSON functions

SELECT extract_json('{"a": 1, "b": 2, "c": 3}', '$.a') FROM source;

extract_json_string returns an unescaped string for the first string match at that JsonPath.

D1: json_array_length with JSON path strings

Section titled “D1: json_array_length with JSON path strings”

D1 documents array sizing with optional JsonPath-style paths inside the function (distinct from Analytics top-level keys):

You can get the length of a JSON array in two ways:

By calling json_array_length(value) directly By calling json_array_length(value, path) to specify the path to an array within an object or outer array.

— Cloudflare, Query JSON

For a column login_history containing previous_logins as an array:

json_array_length(login_history, '$.previous_logins') --> returns 3 as an INTEGER

You can use json_array_length in predicates, for example WHERE json_array_length(some_column, '$.path.to.value') >= 5.

Ruleset Engine: lookup_json_integer and lookup_json_string

Section titled “Ruleset Engine: lookup_json_integer and lookup_json_string”

In Rules language functions, JSON in a string field is read with lookup_json_*, not SQL json_get. The field must be a string representation of valid JSON.

lookup_json_integer and lookup_json_string take the field plus one or more keys (attribute names) or zero-based array indices, walking the document hierarchy:

The key can be an attribute name, a zero-based position number in a JSON array, or a combination of these two options (as extra function parameters), while following the hierarchy of the JSON document to obtain a specific integer value.

— Cloudflare, Functions

Examples from the same page: lookup_json_integer(http.request.body.raw, "version") on { "record_id": "aed53a", "version": 2 } returns 2; nested lookup_json_integer(..., "product", "id") on { "product": { "id": 356 } } returns 356; array index lookup_json_integer(..., 1) on ["first_item", -234] returns -234.

For strings, lookup_json_string(http.request.body.raw, "company") == "cloudflare" illustrates top-level and nested comparisons.

Use these lookups in rule expressions (for example on http.request.body.raw), not in Basin or Analytics SQL SELECT lists.

Basin SQL: map_extract, structs, and get_field

Section titled “Basin SQL: map_extract, structs, and get_field”

Complex types cover MAP columns, not raw JSON strings:

SELECT map_extract(metadata, 'source') AS source,
map_extract(metadata, 'store_name') AS store
FROM my_namespace.products
LIMIT 5

For named structs, scalar functions provide get_field:

Extracts a field from a struct by name.

— Cloudflare, Scalar functions

SELECT get_field(named_struct('customer', customer_id, 'amount', total_amount), 'amount') AS amt
FROM my_namespace.sales_data
WHERE total_amount IS NOT NULL
LIMIT 1

Prefer map_extract / get_field when the schema is typed as map or struct; use json_* when the column is JSON text or a JSON type documented for those functions.

Choosing an API: compare syntax and nesting

Section titled “Choosing an API: compare syntax and nesting”
Surface Typical field Nested access Path style Main limits
Analytics SQL API attributes JSON No (exact top-level key only) attributes['http.method'] No casts, nested traversal, many JSON expressions
Basin SQL JSON column doc Yes, via json_get path args Key paths; json_as_text Use typed json_get_* for stable types
Basin Pipelines JSON string in SQL Yes; also JsonPath json_get, ->, extract_json('$.a') Typed getters return defaults when missing
D1 JSON column Via functions such as json_array_length '$.previous_logins' inside function See Query JSON for supported helpers
Ruleset Engine String JSON on request Yes, chained keys/indices lookup_json_string(field, "a", "b") Integers only for lookup_json_integer; not SQL

Common mistakes

  • Treating attributes['http.method'] in Analytics as nested dot navigation—it is a single key name (data types).
  • Using json_get without json_get_int / json_get_str when downstream logic needs a fixed type (scalar functions).
  • Expecting lookup_json_integer to read 42.0 from JSON (functions).
  • Mixing Rules language lookup_json_* syntax with Basin SQL json_get in the same query—they apply in different execution contexts.
  1. Identify which Cloudflare surface runs your query (Analytics SQL API, Basin SQL, D1, Basin Pipelines, or Ruleset Engine) and open the matching JSON section linked above.

  2. Run a minimal SELECT that projects one known key using the documented syntax (for example attributes['yourKey'], json_get_str(doc, 'name'), or lookup_json_string(http.request.body.raw, "field")).

  3. For Analytics, confirm the key is top-level and that you are not using unsupported constructs (CASE on dynamic JSON keys, nested traversal, and similar limits in data types).

  4. For rules, validate expressions in the Cloudflare dashboard rule editor or test harness so lookup_json_* types match the JSON you receive.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match the linked Cloudflare documentation, email yimasynthetic@gmail.com with the page URL and the exact statement you believe is wrong or unclear.