I have a column with json
type in MySQL database and the stored data structure is as follows:
table row 1:
{
"products":[
{
"price_c":"800",
"item":"cloth"
},
{
"price_f":"100",
"item":"food"
}
]
}
table row 2:
{
"products":[
{
"price_c":"600",
"item":"cloth"
},
{
"price_f":"200",
"item":"food"
}
]
}
I have been trying to extract the price
of ass "item":"food"
and did that with the query:
SELECT column->>'$**.price_f' as Price FROM table
where JSON_CONTAINS(column, '{"item":"food"}', '$.products');
But the problem with this approach is that it returns
+---------+
| Price |
+---------+
| ["100"] |
| ["200"] |
+---------+
Although I have used ->>
but still the result is not unquoted and I have figured out that it is probably due to the wildcard $**.price
. I have tried JSON_UNQUOTE
and it also doesn't work. How should I unquote the outputs?
Here is the sample project in dbfiddle
It is a string , so use string functions to remove the unwanted character
SELECT REGEXP_REPLACE(col1->>'$**.price_f' ,'[^0-9]','') as Price FROM tab1 where JSON_CONTAINS(col1, '{"item":"food"}', '$.products');
\n| Price |\n| :---- | \n| 200 | \n| 100 | \n
db<>fiddle here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.