Using Mariadb 10.6 – In the following example, I try to convert the entries of the json object into table rows:
SELECT *
FROM JSON_TABLE('{
"1": [1, 123.25],
"10": [2, 110.5],
"100": [3, 105.75]
}', '$.*' COLUMNS (
col1 decimal(13,2) PATH '$',
col2 int PATH '$[0]',
col3 decimal(17,2) PATH '$[1]'
)) table1
The result is:
col1 | col2 | col3 |
---|---|---|
NULL | 1 | 123.25 |
NULL | 2 | 110.50 |
NULL | 3 | 105.75 |
Is there any way to fill "col1" with the property keys ("1", "10", "100")?
I guess there is some "key word" to reference the key, but I can’t find any information on this in the docs from MariaDB or MySQL.
I already made a routine that creates a temporary table by looping output from JSON_KEYS
, but it would be more elegant if I can use JSON_TABLE
for this job.
3
Answers
I answer to my own question: I'm sorry that there apparently is'nt any native option to reference the key names in a json object with
JSON_TABLE
(yet), and the two workarounds currently posted are great. I ended up using a mixture from both:Here’s one way to do it without routines:
JSON_TABLE
, alongside a row number usingFOR ORDINALITY
JSON_KEYS
JSON_EXTRACT
Output:
Check the demo here.
This is an other way to do it using
CROSS JOIN
,JSON_TABLE
&JSON_KEYS
:JSON_KEYS(json)
will give us["1", "10", "100"]
CROSS JOIN
is used to generate multi rows from["1", "10", "100"]
demo here