I have the following entry in a JSON field in MySQL, How can I access the wid_1 -> pct_usage data?
{ "wid_1": { "cof": 1, "pct_usage": 50.0, "tid": 69285334, "wid": 1 }, "wid_2": { "cof": 1, "pct_usage": 50.0, "tid": 69285335, "wid": 2 } }
I tried various variations of:
json_unquote(json_extract(s.`col_name`,'$.wid_1'.'pct_usage')) as wid_1_pct,
json_unquote(json_extract(s.`col_name`,'$.wid_1','pct_usage')) as wid_1_pct,
json_unquote(json_extract(s.`col_name`,'$.wid_1'{'pct_usage'})) as wid_1_pct,
json_unquote(json_extract(s.`col_name`,'$.wid_1','{pct_usage}')) as wid_1_pct,
but none of them work.
asked Apr 14, 2023 at 10:40
1 Answer 1
This one works for me:
SELECT s.col_name,
json_extract(s.col_name, '$.wid_1.pct_usage') as wid_1_pct
FROM t1 as s
answered Apr 14, 2023 at 12:07
-
1one of the few variations I didn't try, cheers.IGGt– IGGt2023年04月14日 13:08:39 +00:00Commented Apr 14, 2023 at 13:08
lang-sql
pct_usage
as a regular column.