Search code examples
sqlamazon-web-servicesamazon-redshift

Extract data dynamically in Amazon Redshift


This is the sample data in the column. I want to extract the values only associated with 5 in dynamically.

'{"2113":5,"2112":5,"2114":4,"2511":5}'

The final structure should be 3 rows of names and values?

I tried with JSON extract function but that not help. Thanks

Final result i want,

  value | Key
  2113     5
  2112     5
  2115     5                                                                        

Solution

  • Achieved the result using multiple splits.

    `SELECT  distinct split_part(split_part(replace(replace(replace(json_field,'{',''),'}',''),'"',''),',',i),': ',1) as value,` `split_part(split_part(replace(replace(replace(json_field,'{',''),'}',''),'"',''),',',i),':',2) as key FROM table
      JOIN schema.seq_1_to_100 as numbers
      ON i <=regexp_count(json_field,':') `