JSON_GET_ATTRIBUTE
Supported by ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
JSON object attributes can be accessed by key as follows:
SELECT
'{"a":"b"}'::json->'a'
create.select(jsonGetAttribute(json("{\"a\":\"b\"}"), "a"))
.fetch();
The result would look like this:
+--------------------+ | json_get_attribute | +--------------------+ | "b" | +--------------------+
The API is PostgreSQL inspired, and as such, there are two ways of accessing attributes:
-
->or withjsonGetAttribute()/jsonbGetAttribute(): To produce aorg.jooq.JSONororg.jooq.JSONBvalue -
->>or withjsonGetAttributeAsText()/jsonbGetAttributeAsText(): To produce ajava.lang.Stringvalue (see JSON_GET_ATTRIBUTE_AS_TEXT)
Dialect support
This example using jOOQ:
jsonGetAttribute(json("{\"a\":1}"), "a")
Translates to the following dialect specific expressions:
Aurora Postgres, CockroachDB, Postgres, YugabyteDB
(cast('{"a":1}' AS json)->'a')
BigQuery
('{"a":1}')['a']
ClickHouse
JSONExtractRaw('{"a":1}', 'a')
DB2 11+
coalesce(
coalesce(
json_query(
'{"a":1}',
('$.' || cast('a' AS varchar(3998)))
),
CASE
WHEN json_value(
'{"a":1}',
('$.' || cast('a' AS varchar(3998)))
DEFAULT 'empty' ON EMPTY
) IS NULL THEN 'null'
END
),
nvl2(
json_query(
'{"a":1}',
('$.' || cast('a' AS varchar(3998))) EMPTY ARRAY ON EMPTY
),
NULL,
'null'
)
)
DuckDB
(JSON '{"a":1}'->'a')
MariaDB 10.2+
json_extract(
json_extract('{"a":1}', '$'),
concat('$.', 'a')
)
MySQL 5.7+
json_extract(
cast('{"a":1}' AS json),
concat('$.', 'a')
)
Oracle
json_query(
'{"a":1}',
'$.a'
)
SQLite 3.38+
(JSON('{"a":1}')->'a')
Snowflake
get(parse_json('{"a":1}'), 'a')
Spanner
(JSON '{"a":1}')['a']
Trino
json_extract(
json_parse('{"a":1}'),
('$.' || 'a')
)
ASE, Access, Aurora MySQL, DB2 9 to 10, Databricks, Exasol, Firebird, H2, HSQLDB, Hana, Informix, MariaDB 10.0 to 10.1, MemSQL, MySQL 5.6, Redshift, SQLDataWarehouse, SQLServer, SQLite 3.25 to 3.30, Sybase, Teradata, Vertica
/* UNSUPPORTED */
Generated with jOOQ 3.22. Support in older jOOQ versions may differ. Translate your own SQL on our website
References to this page
Feedback
Do you have any feedback about this page? We'd love to hear it!