The redshift_connector supports the Amazon Redshift SUPER datatype for handling semistructured JSON data.
Sending Data
To send data to a SUPER column, pass a string containing JSON data. It is recommended to use the Redshift json_parse(%s) function within your SQL statement to convert the string into the SUPER type.
Receiving Data
SUPER data is received as a string containing the JSON data.
import redshift_connector
import json
# Example: Creating a table with SUPER data using json_parse
with redshift_connector.connect(...) as conn:
with conn.cursor() as cursor:
cursor.execute(
"CREATE TABLE foo AS SELECT json_parse(%s) AS multi_level_array;",
('[[1.1, 1.2], [2.1, 2.2], [3.1, 3.2]]',
)
cursor.execute("SELECT array, element FROM foo AS f, f.multi_level_array AS array, array AS element;")
result = cursor.fetchall()
print(result)
# Example: Retrieving specific elements from a JSON object stored in SUPER
with redshift_connector.connect(...) as conn:
with conn.cursor() as cursor:
cursor.execute(
"CREATE TABLE foo AS SELECT json_parse(%s) AS vals;",
(json.dumps({"x": [1,2,3,4], "y": [5,6,7,8], "z": [9,10,11,12]}),)
)
cursor.execute("SELECT vals.x FROM foo;")
result = cursor.fetchall()
print(result)