Available in versions: Dev (3.22) | Latest (3.21) | 3.20 | 3.19 | 3.18

JSON_GET_ATTRIBUTE_AS_TEXT

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(jsonGetAttributeAsText(json("{\"a\":\"b\"}"), "a"))
      .fetch();

The result would look like this:

+----------------------------+
| json_get_attribute_as_text |
+----------------------------+
| b                          |
+----------------------------+

The API is PostgreSQL inspired, and as such, there are two ways of accessing attributes:

Dialect support

This example using jOOQ:

jsonGetAttributeAsText(json("{\"a\":1}"), "a")

Translates to the following dialect specific expressions:

Aurora Postgres, CockroachDB, Postgres, YugabyteDB

(cast('{"a":1}' AS json)->>'a')

BigQuery

coalesce(
  json_value(
    ('{"a":1}')['a'],
    '$'
  ),
  nullif(
    to_json_string(('{"a":1}')['a']),
    'null'
  )
)

ClickHouse

JSONExtractString('{"a":1}', 'a')

DB2 11+

coalesce(
  json_value(
    '{"a":1}',
    ('$.' || cast('a' AS varchar(3998)))
  ),
  json_query(
    '{"a":1}',
    ('$.' || cast('a' AS varchar(3998)))
  )
)

Databricks

get_json_object(
  '{"a":1}',
  ('$.' || 'a')
)

DuckDB

(JSON '{"a":1}'->>'a')

MariaDB 10.2+

json_unquote(nullif(
  cast(
    json_extract(
      json_extract('{"a":1}', '$'),
      concat('$.', 'a')
    )
    AS char
  ),
  'null'
))

MySQL 5.7+

json_unquote(nullif(
  json_extract(
    cast('{"a":1}' AS json),
    concat('$.', 'a')
  ),
  cast('null' AS json)
))

Oracle

coalesce(
  json_value(
    '{"a":1}',
    '$.a'
  ),
  nullif(
    json_query(
      '{"a":1}',
      '$.a'
    ),
    'null'
  )
)

SQLServer 2016+

coalesce(
  json_query(
    '{"a":1}',
    ('$.' + 'a')
  ),
  json_value(
    '{"a":1}',
    ('$.' + 'a')
  )
)

SQLite

json_extract(
  JSON('{"a":1}'),
  ('$.' || 'a')
)

Spanner

coalesce(
  json_value(
    (JSON '{"a":1}')['a'],
    '$'
  ),
  nullif(
    to_json_string((JSON '{"a":1}')['a']),
    'null'
  )
)

Trino

coalesce(
  json_extract_scalar(
    json_parse('{"a":1}'),
    ('$.' || 'a')
  ),
  json_format(nullif(
    json_extract(
      json_parse('{"a":1}'),
      ('$.' || 'a')
    ),
    json_parse('null')
  ))
)

ASE, Access, Aurora MySQL, DB2 9 to 10, Exasol, Firebird, H2, HSQLDB, Hana, Informix, MariaDB 10.0 to 10.1, MemSQL, MySQL 5.6, Redshift, SQLDataWarehouse, SQLServer 2012 to 2014, Snowflake, Sybase, Teradata, Vertica

/* UNSUPPORTED */
Generated with jOOQ 3.21. 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!

The jOOQ Logo