如何在 MariaDB 中将值转换为 JSON?

How to cast value as JSON in MariaDB?

我想提取一个 JSON,名称为 table,每个 table 中都有列。

这是我的 SQL:

SELECT
    JSON_OBJECTAGG(table_name, columns)
FROM (
    SELECT
        table_name,
        JSON_OBJECTAGG(column_name, data_type) as columns
    FROM `COLUMNS`
    WHERE
        `TABLE_SCHEMA` = 'my_db'
    GROUP BY table_name
) table_columns

问题出在JSON_OBJECTAGG(table_name, columns),列变成了字符串。如何将其转换为 JSON?

使用JSON_EXTRACT(column_value_in_json, '$')

SELECT
    JSON_OBJECTAGG(table_name,
        JSON_EXTRACT(
           columns,
           '$'
        )
    )
FROM (
    SELECT
        table_name,
        JSON_OBJECTAGG(column_name, data_type) as columns
    FROM `COLUMNS`
    WHERE
            `TABLE_SCHEMA` = 'my_db'
    GROUP BY table_name
) table_columns