Skip to content

JSON_TABLE

JSON_TABLE(expr, path COLUMNS (column_list) [AS] alias)

Section titled “JSON_TABLE(expr, path COLUMNS (column_list) [AS] alias)”
  • expr: This parameter represents the JSON document that needs to be converted to a relational form. It is an SQL expression that returns a JSON value. Usually, a subquery or column name is used.
  • path: It is a JSON path expression that specifies the object or the array in the JSON data to extract. ’$’ refers to the entire JSON document.
  • columns (column_list): This option defines a list of columns to be produced by the function. Each column in the list should have a name and a data type. The syntax is “name type PATH string”.
  • [as] alias: This optional parameter is used to set an alias name for the JSON_TABLE. This can be particularly useful in situations when querying multiple tables at once, as it helps to avoid any confusion emanating from having columns with the same name in different tables.
SELECT *
FROM JSON_TABLE(
'{"partner": "Google", "value": 18}',
'$' COLUMNS(
partner VARCHAR(255) PATH '$.partner',
value VARCHAR(255) PATH '$.value'
)
) AS jt;
+--------+-------+
| partner | value |
+--------+-------+
| Google | 18 |
+--------+-------+

The JSON_TABLE function in MySQL takes a JSON object and transforms it into a relational format. In this example, it takes a JSON object with keys ‘partner’ and ‘value’, and creates a table where each key corresponds to a separate column. The key value pairs in the JSON object become the column name and value in the output table.