I have a H2 database (version 2.2.220) with data in a JSON column; I need to migrate the column via a SQL script so that the data is in a conventional relational database structure. The main stumbling block is that on extraction of a JSONValue string value (surrounded by double quotes in JSON format), I am struggling to convert this into a VARCHAR value that does not include the surrounding double quotes.
I hope the below SQL statements illustrate the issue in the most rudimentary way:
-
The double quotes inside the string constant is necessary, else it is not recognized as valid JSON.create table my_table ( id int generated by default as identity, my_json JSON null default null, ); insert into my_table (my_json) values (JSON '"abcdef"'); - 1st query (naive):
This returnsselect my_json from my_table;"abcdef"with quotes included in the string. I would however like the raw string valueabcdefto be returned without the surrounding quotes. - 2nd query (typecasting):
This still returnsselect CAST(my_json as VARCHAR(255)) from my_table;"abcdef"with quotes included in the string. - 3rd query ("brute-force" trimming):
This achieves the desired resultselect BTRIM(my_json, '"') from my_table;abcdef, but is problematic. One way this will cause a bug is when the string value should intentionally include a double quote at the beginning and/or end, e.g.xyz"(JSON '"xyz\""') - this will be trimmed to returnxyz\.
I am wondering if there is a "recommended"/cleaner way to do this conversion that is less bug-prone.