Convertion of tabular data to JSON in Redshift

Viewed 4877

I am unable to figure out how to convert tabular data to JSON format and store it in another table in Redshift. For example, I have a "DEMO" table with four columns: pid,stid,item_id,trans_id.

For each combination of pid,stid,item_id there exist many trans_ids.

pid  stid  item_id  trans_id :

1 , AB  , P1 , T1  
1 , AB  , P1 , T2  
1 , AB  , P1 , T3     
1 , AB  , P1 , T4   
2 , ABC , P2 , T5  
2 , ABC , P2 , T6  
2 , ABC , P2 , T7  
2 , ABC , P2 , T8

I want to store this data in another table called "SAMPLE" as:

pid  stid  item_id      trans_id

1 , AB  , P1 , {"key1":T1, "key2":"T2" "key2":"T3" "key2":"T4"}    
2 , ABC , P2 , {"key1":T5, "key2":"T6" "key2":"T7" "key2":"T8"}

I am unable to figure out how to load the data from "DEMO" to "SAMPLE" in JSON format only for column "trans_id" using a SQL query in Redshift. I don't want to use any intermediate files.

2 Answers
Related