Redshift 获取 Json 密钥

Mig*_*uel 4 sql json amazon-web-services amazon-redshift

我有一个表字段,其值如下:

{
    "0fc8a2a1-e334-43b8-9311-ce46da9cd32c": {
        "alert": "345",
        "channel": "ios_push",
        "name": "Variant 1"
    },
    "4344d89b-7f0d-4453-b2c5-d0d4a39d7d25": {
        "channel": "ios_push",
        "name": "Control Group",
        "type": "control"
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有某种方法可以获得“0fc8a2a1-e334-43b8-9311-ce46da9cd32c”“4344d89b-7f0d-4453-b2c5-d0d4a39d7d25”值。

nbt*_*btk 7

虽然这个问题提出已经过去一年了,但我发现了如何使用 Redshift 原生功能来使用它。

Redshift 有一个存储本机 JSON 的 SUPER 列类型: https://docs.aws.amazon.com/redshift/latest/dg/r_SUPER_type.html

并可以使用 PartiQL 进行查询:

https://docs.aws.amazon.com/redshift/latest/dg/query-super.html

https://partiql.org/tutorial.html

MyTable假设使用名为 SUPER 类型的列调用的表data可以存储 JSON,我创建了以下查询:

SELECT
  key
FROM
  MyTable AS t,
  UNPIVOT t.data AS value AT key;
Run Code Online (Sandbox Code Playgroud)
  • 对于 JSON 数组,语法x AS y AT z意味着数组中对象的索引在"foreach Y in X"哪里zyx
  • x在逆透视 JSON 对象的情况下,和 的语法含义相同y,但z是键,而不是索引(当你想到这一点时,数组也可以以对象方式表示,其中索引是键:`{ 0: 'a', 1: 'b', ... }

查询结果为:

create temp table if not exists MyTable(
  data SUPER
);
insert into MyTable VALUES (json_parse('{"a": 1, "b":2}'));

select key from MyTable as t, unpivot t.data as value at key;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述