'PrestoSQL comma-delimited varchar column to JSON array
I have a column in a table extracted from json using json_extract_scalar(json_parse(<my json array>), '$.column')
Now I have a column in the table that is a varchar (I believe) and looks like:
| column |
|---|
| word1, word2, word3, word4 |
I want to use this column later and put it in a nested JSON array, but if I put it into a json array as it is right now, it will look like:
"column":"word1, word2, word3, word4" in the json array, when I instead want:
"column": ["word1", "word2", "word3", "word4"]
So, my question is how do I make the column into a JSON array/list first so that the output is what I want when I later put it in an array?
I have tried doing CAST(column AS JSON) but it gives me:
| column |
|---|
| "word1, word2, word3, word4" |
I'm using Presto SQL
Solution 1:[1]
CAST(SPLIT(json_extract_scalar(json_parse(<my json array>), '$.column'), ',') AS JSON) gave me the output I desired
Solution 2:[2]
You need to convert you string into an array, for example using split:
select cast(split('word1, word2, word3, word4', ',') as json)
Output:
| _col0 |
|---|
| ["word1"," word2"," word3"," word4"] |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | codenoodles |
| Solution 2 | Guru Stron |
