简体   繁体   中英

Get specific values from JSON column in Presto

I have a table with a JSON column points with one of the rows as:

{"0": 0.2, "1": 1.2, "2": 0.5, "15": 1.2, "20": 0.7}

I want to get the values for keys "1" and "20" and store them as an alias like first and second in a query. What I've done till now is:

SELECT points, k, v from rewards CROSS JOIN UNNEST(SPLIT_TO_MAP(points, ',', ':')) AS m(k,v) where name='John'

But this query gives me all the rows of k, v. How do I select only those two values corresponding to "1" and "20"?

JSON_EXTRACT_SCALAR达到了目的。

JSON_EXTRACT_SCALAR(points, '$["1"]') AS first_value

JSON_EXTRACT_SCALAR(points, '$["20"]') AS second_value

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM