我想把这个JSON数据放到一个Redshift表中,其中包含了产品、价格和数量列。我应该如何使用Redshift的JSON函数来做到这一点?https://docs.aws.amazon.com/redshift/latest/dg/json-functions.html
{ "lamp": { "price": 18.99, "quantity": 30 }, "desk": { "price": 129.99, "quantity": 12 }, "vase": { "price": 22.49, "quantity": 18 }, "speakers": { "price": 49.99, "quantity": 50 } }
我尝试过使用JSON_EXTRACT_PATH_TEXT函数,但我不确定如何将它应用于这些数据(例如,它可以使用通配符来执行迭代吗?)
发布于 2022-11-01 13:03:05
您可以使用JSON_PARSE而不是JSON_EXTRACT_PATH_TEXT,它返回可以轻松访问json对象的超级数据类型。我不知道你从哪里得到数据,但这是我的例子。
使用示例创建表:
create table test.temp_json
(
json_super super
);
insert into test.temp_json
values (json_parse('{ "lamp": { "price": 18.99, "quantity": 30 }, "desk": { "price": 129.99, "quantity": 12 }, "vase": { "price": 22.49, "quantity": 18 }, "speakers": { "price": 49.99, "quantity": 50 } }'))
存取数据:
select key as product, value.price as price, value.quantity as quantity
from test.temp_json j,
UNPIVOT j.json_super AS value AT key;
结果:
F 211
阅读更多关于json_parse和un枢轴https://docs.aws.amazon.com/redshift/latest/dg/JSON_PARSE.html an https://docs.aws.amazon.com/redshift/latest/dg/query-super.html#unpivoting的信息
https://stackoverflow.com/questions/74262629
复制相似问题