Convert JSON data to a relational table. This table function extracts data from a JSON document and returns it as a relational table with columns.
JSON_TABLE(json_doc,
context_path COLUMNS (column_list)
) [AS] aliascolumn_list:
column[, column][, ...]column:
name FOR ORDINALITY
| name type PATH path_str [on_empty] [on_error]
| name type EXISTS PATH path_str
| NESTED PATH path_str COLUMNS (column_list)on_empty:
{NULL | DEFAULT string | ERROR} ON EMPTYon_error:
{NULL | DEFAULT string | ERROR} ON ERRORname type PATH path_str [on_empty] [on_error]SET @json='
[
{"name":"Laptop", "color":"black", "price":"1000"},
{"name":"Jeans", "color":"blue"}
]';
SELECT * FROM json_table(@json, '$[*]'
COLUMNS(
name VARCHAR(10) path '$.name',
color VARCHAR(10) path '$.color',
price DECIMAL(8,2) path '$.price' )
) AS jt;
+--------+-------+---------+
| name | color | price |
+--------+-------+---------+
| Laptop | black | 1000.00 |
| Jeans | blue | NULL |
+--------+-------+---------+name FOR ORDINALITYset @json='
[
{"name":"Laptop", "color":"black"},
{"name":"Jeans", "color":"blue"}
]';
select * from json_table(@json, '$[*]'
columns(
id for ordinality,
name varchar(10) path '$.name')
) as jt;
+------+--------+
| id | name |
+------+--------+
| 1 | Laptop |
| 2 | Jeans |
+------+--------+name type EXISTS PATH path_strset @json='
[
{"name":"Laptop", "color":"black", "price":1000},
{"name":"Jeans", "color":"blue"}
]';
select * from json_table(@json, '$[*]'
columns(
name varchar(10) path '$.name',
has_price integer exists path '$.price')
) as jt;
+--------+-----------+
| name | has_price |
+--------+-----------+
| Laptop | 1 |
| Jeans | 0 |
+--------+-----------+NESTED PATH path COLUMNS (column_list)SET @json='
[
{"name":"Jeans", "sizes": [32, 34, 36]},
{"name":"T-Shirt", "sizes":["Medium", "Large"]},
{"name":"Cellphone"}
]';select * from json_table(@json, '$[*]'
columns(
name varchar(10) path '$.name',
nested path '$.sizes[*]' columns (
size varchar(32) path '$'
)
)
) as jt;
+-----------+--------+
| name | size |
+-----------+--------+
| Jeans | 32 |
| Jeans | 34 |
| Jeans | 36 |
| T-Shirt | Medium |
| T-Shirt | Large |
| Cellphone | NULL |
+-----------+--------+set @json='
[
{"name":"Jeans", "sizes": [32, 34, 36], "colors":["black", "blue"]}
]';
select * from json_table(@json, '$[*]'
columns(
name varchar(10) path '$.name',
nested path '$.sizes[*]' columns (
size varchar(32) path '$'
),
nested path '$.colors[*]' columns (
color varchar(32) path '$'
)
)
) as jt;
+-------+------+-------+
| name | size | color |
+-------+------+-------+
| Jeans | 32 | NULL |
| Jeans | 34 | NULL |
| Jeans | 36 | NULL |
| Jeans | NULL | black |
| Jeans | NULL | blue |
+-------+------+-------+on_empty:
{NULL | DEFAULT string | ERROR} ON EMPTYon_error:
{NULL | DEFAULT string | ERROR} ON ERRORSELECT * FROM JSON_TABLE('{"foo": [1,2,3,4]}','$' columns( jscol json path '$.foo') ) AS T;
+-----------+
| jscol |
+-----------+
| [1,2,3,4] |
+-----------+SELECT * FROM JSON_TABLE('{"foo": [1,2,3,4]}','$' columns( jscol json path '$.foo') ) AS T;
+-------+
| jscol |
+-------+
| NULL |
+-------+