从 Redshift table 获取 JSON 数据
Getting JSON Data from Redshift table
我想从 redshift table 中检索 SQL 中的数据作为 json 字符串。有没有可以做到这一点的功能?
Postgres 具有 row_to_json 函数,可用于 SQL 语句以获取数据,如 JSON.Are 在 Redshift 中是否有等效项?
根据您想执行的操作,您可以使用以下功能之一:
http://docs.aws.amazon.com/redshift/latest/dg/json-functions.html
JSON_EXTRACT_PATH_TEXT
很有用 http://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_PATH_TEXT.html
select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}','f4', 'f6');
json_extract_path_text
----------------------
star
我仍在尝试弄清楚如何在没有复合类型或数组的情况下不必列出每个项目,但这应该可以帮助您生成 json 而无需执行字符串内容。
CREATE OR REPLACE FUNCTION test_function
(userid int, firstname varchar, lastname varchar, email varchar)
RETURNS varchar(max) immutable as $$
import json
o = {
"userid": userid,
"firstname": firstname,
"lastname": lastname,
"email": email
}
return json.dumps(o)
$$ LANGUAGE plpythonu;
我想从 redshift table 中检索 SQL 中的数据作为 json 字符串。有没有可以做到这一点的功能?
Postgres 具有 row_to_json 函数,可用于 SQL 语句以获取数据,如 JSON.Are 在 Redshift 中是否有等效项?
根据您想执行的操作,您可以使用以下功能之一: http://docs.aws.amazon.com/redshift/latest/dg/json-functions.html
JSON_EXTRACT_PATH_TEXT
很有用 http://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_PATH_TEXT.html
select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}','f4', 'f6');
json_extract_path_text
----------------------
star
我仍在尝试弄清楚如何在没有复合类型或数组的情况下不必列出每个项目,但这应该可以帮助您生成 json 而无需执行字符串内容。
CREATE OR REPLACE FUNCTION test_function
(userid int, firstname varchar, lastname varchar, email varchar)
RETURNS varchar(max) immutable as $$
import json
o = {
"userid": userid,
"firstname": firstname,
"lastname": lastname,
"email": email
}
return json.dumps(o)
$$ LANGUAGE plpythonu;