简体   繁体   English

从Presto的JSON列获取特定值

[英]Get specific values from JSON column in Presto

I have a table with a JSON column points with one of the rows as: 我有一个JSON列的表points与行为之一:

{"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. 我想获取键"1""20" ,并将它们存储为别名,例如查询中的firstsecond 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"? 但是此查询为我提供了k,v的所有行。如何仅选择对应于“ 1”和“ 20”的那两个值?

JSON_EXTRACT_SCALAR达到了目的。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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