如何在Redshift中从Json提取项目值

时间:2018-07-13 10:05:59

标签: amazon-web-services amazon-redshift

Redshift表仅包含2列:

PID str_val
1   {{"history":[{"value":"1500500.0","date":"2017-11-01"},{"value":"1614800.0","date":"2018-06-01"},{"value":"1363700.0","date":"2017-07-01"}]
2   {{"history":[{"value":"1500500.0","date":"2017-11-01"},{"value":"1614800.0","date":"2018-06-01"},{"value":"133300.0","date":"2017-07-01"}]
3   {{"history":[{"value":"1500500.0","date":"2017-11-01"},{"value":"1614800.0","date":"2018-06-01"},{"value":"1345700.0","date":"2017-07-01"}]

我当前正在使用:

    select json_extract_path_text(string_val, 'history','value') as key2 from property_avm_json

,但没有将值作为列。如何从此json数组中提取值键? 我在这里关注AWS文档-https://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_PATH_TEXT.html

它确实为我提供了历史价值,但在查询中没有“价值”吗?如何获得

1 个答案:

答案 0 :(得分:0)

[]中的元素是一个数组,需要使用其他函数进行访问。 https://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_ARRAY_ELEMENT_TEXT.html

您可以嵌套函数以获取所需的数据

SELECT json_extract_path_text( 
         json_extract_array_element_text( 
           json_extract_path_text( 
             '{"history":[{"value":"1500500.0","date":"2017-11-01"},{"value":"1614800.0","date":"2018-06-01"},{"value":"1363700.0","date":"2017-07-01"}]}'
             , 'history') 
           ,1 ) 
         ,'value')  as key2 ;
--   key2
-------------
-- 1614800.0