Parsing JSON in MariaDB with SQLPage #146
-
Hi ! I'm working with MariaDB, and I'm struggling with the JSON files produced by a checkbox in a form component. The doc states that I can use :
I see perfectly the problem (SQL page cannot parse this syntax), but I cannot understand what is meant by 'wrapping this in a function'... (I am also quite new to Mariadb). So any help / suggestion is welcome. Actually, I would just need to rewrite in mariadb/mysql the following piece of code from the doc : insert into best_fruits(id)
select CAST(value AS integer) as id
from *json_each*($preferred_fruits); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hello and welcome to SQLPage ! I added an example to the documentation which hopefully illustrates your use case: https://github.com/lovasoa/SQLpage/tree/main/examples/mysql%20json%20handling insert into group_members(group_id, user_id)
select CAST(json_unquote(json_elems.json_value) AS INT), last_insert_id()
from (
with recursive json_elems(n, json_value) as (
select 0, json_extract(:Memberships, '$[0]')
union all
select n + 1, json_extract(:Memberships, concat('$[', n + 1, ']'))
from json_elems
where json_value is not null
) select * from json_elems where json_value is not null
) as json_elems
where :Memberships is not null; Let me know if it works for you. In the long term we will support the What I initially meant was something like CREATE PROCEDURE insert_group_members_json (in userid INT, in group_members_json TEXT)
BEGIN
INSERT INTO group_members(user_id, group_id)
select userid, CAST(json_val AS INT)
from json_table(
group_members_json,
"$[*]" COLUMNS(json_val VARCHAR(255) PATH "$")
) as json_t;
END; but it would also be impossible to call from sqlpage |
Beta Was this translation helpful? Give feedback.
-
I wrote the code for parsing |
Beta Was this translation helpful? Give feedback.
-
SQLPage v0.18.3 was just released, with support for MySQL's |
Beta Was this translation helpful? Give feedback.
Hello and welcome to SQLPage !
I added an example to the documentation which hopefully illustrates your use case: https://github.com/lovasoa/SQLpage/tree/main/examples/mysql%20json%20handling
Let me know if…