Available in versions: Dev (3.22) | Latest (3.21) | 3.20 | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12
JSON_OBJECT
Supported by ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
The JSON_OBJECT function is used to produce simple JSON objects from scalar values, without aggregation(see also JSON_OBJECTAGG)
SELECT json_object( KEY 'firstName' VALUE author.first_name, KEY 'lastName' VALUE author.last_name ) FROM author
create.select(jsonObject(
key("firstName").value(AUTHOR.FIRST_NAME),
key("lastName").value(AUTHOR.LAST_NAME)))
.from(AUTHOR)
.fetch();
The result would look like this:
+--------------------------------------------+
| json_object |
+--------------------------------------------+
| {"firstName":"Paulo","lastName":"Coelho"} |
| {"firstName":"George","lastName":"Orwell"} |
+--------------------------------------------+
Dialect support
This example using jOOQ:
jsonObject("firstName", AUTHOR.FIRST_NAME)
Translates to the following dialect specific expressions:
Aurora Postgres, CockroachDB, Postgres, YugabyteDB 2.25+
json_build_object('firstName', AUTHOR.FIRST_NAME)
BigQuery, DuckDB, MariaDB 10.2+, MySQL 5.7+, SQLite, Spanner
json_object('firstName', AUTHOR.FIRST_NAME)
ClickHouse
toJSONString(map('firstName', AUTHOR.FIRST_NAME))
DB2 11+, H2, Oracle
json_object(KEY 'firstName' VALUE AUTHOR.FIRST_NAME)
Databricks
to_json(map_from_entries(ARRAY(struct( 'firstName', cast(AUTHOR.FIRST_NAME AS variant) ))))
SQLServer 2022+
json_object('firstName': AUTHOR.FIRST_NAME)
SQLServer 2016 to 2017
(
SELECT (
SELECT *
FROM (
VALUES (AUTHOR.FIRST_NAME)
) t (firstName)
FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER
)
)
Snowflake
object_construct_keep_null('firstName', AUTHOR.FIRST_NAME)
Trino
cast(
map_from_entries(ARRAY[row(
'firstName',
cast(AUTHOR.FIRST_NAME AS json)
)])
AS json
)
ASE, Access, Aurora MySQL, DB2 9 to 10, Exasol, Firebird, HSQLDB, Hana, Informix, MariaDB 10.0 to 10.1, MemSQL, MySQL 5.6, Redshift, SQLDataWarehouse, SQLServer 2012 to 2014, Sybase, Teradata, Vertica, YugabyteDB 2.9
/* 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!