【问题标题】:How to extract item values from Json in Redshift如何从 Redshift 中的 Json 中提取项目值
【发布时间】:2018-07-13 10:05:58
【问题描述】:

Redshift 表仅包含 2 列:

PID str_val 1 {{"历史":[{"value":"1500500.0","date":"2017-11-01"},{"value":"1614800.0","date":"2018-06-01" },{"值":"1363700.0","日期":"2017-07-01"}] 2 {{"历史":[{"value":"1500500.0","date":"2017-11-01"},{"value":"1614800.0","date":"2018-06-01" },{"值":"133300.0","日期":"2017-07-01"}] 3 {{"历史":[{"value":"1500500.0","date":"2017-11-01"},{"value":"1614800.0","date":"2018-06-01" },{"值":"1345700.0","日期":"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

它确实给了我历史的价值,而不是查询中的“价值”?如何获得这个

【问题讨论】:

  • 旁注:在 Amazon Redshift 中存储 JSON 数据效率非常低,并且无法从 Redshift 的全部功能中受益。如果您希望经常查询单个元素,建议将数据转换为普通的 Redshift 列(最好在将其加载到 Redshift 之前)。

标签: amazon-web-services amazon-redshift


【解决方案1】:

[] 中的元素是一个数组,需要使用不同的函数来访问。 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

【讨论】:

  • 如何一次获取所有值?在行中
猜你喜欢
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多