【问题标题】:json array extraction in ANSI SQL using Presto使用 Presto 在 ANSI SQL 中提取 json 数组
【发布时间】:2020-09-25 09:48:20
【问题描述】:

这是我遇到的问题: 我的 presto 表中的列“xyz”有 json 格式的数据元素,数组格式如下所述:

col1 | col2
a    | {"fruits": ["apple", "banana", "orange"]}
b    | {"fruits": ["apple", "banana", "orange", "grapes"]}

我需要以下格式的数据:

col1 | col2 | col3 | col4  | col5
a    | apple|banana|orange | null
b    | apple|banana|orange | grapes

任何关于如何在 json 中读取数组元素的示例都会非常有帮助

【问题讨论】:

    标签: arrays json presto


    【解决方案1】:

    您可以使用 json_array_extract_scalar() 函数和每个列的相应索引值来将每个列值提取为字符串

    SELECT col1,
           json_array_extract_scalar(col2,'$.fruits[0]') as col2,
           json_array_extract_scalar(col2,'$.fruits[1]') as col3,
           json_array_extract_scalar(col2,'$.fruits[2]') as col4,
           json_array_extract_scalar(col2,'$.fruits[3]') as col5
      FROM tab
    

    您也可以将上述查询中的json_array_extract_scalar替换为json_array_extract,以获取JSON格式的每一列。

    【讨论】:

    • @Piotr:非常感谢您的回复,但我收到了流动错误:(SYNTAX_ERROR)第 3:2 行:意外参数(行(水果数组(varchar)),varchar(9))对于函数 json_extract
    • 是的@AAN,我们需要稍微转换一下函数名json_array_extract_scalar,而不是json_extract_scalar
    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 2019-03-07
    • 2020-11-07
    • 2021-05-06
    • 2021-05-01
    • 2020-04-18
    • 2019-06-24
    • 2020-12-24
    相关资源
    最近更新 更多