3

I have a field called as genres in a Redshift table. It's stored as a string and not in the form of a JSON array since redshift does not give us that capability.

 [{"id": 27, "name": "Horror"}, {"id": 878, "name": "Science Fiction"}]

I want to extract the elements - 'id' and 'name' and dump it into another table in the below-given format. How do I do it?

reference

AdityaT101
  • 51
  • 1
  • 6
  • this could be helpful: http://torsten.io/stdout/expanding-json-arrays-to-rows/ – mangusta Jul 03 '20 at 02:20
  • This question is already answered here https://stackoverflow.com/questions/30195760/return-elements-of-redshift-json-array-on-separate-rows/30198745#30198745 – botchniaque Jul 03 '20 at 05:52

1 Answers1

2

@botchniaque , @mangusta

Thanks fr all the help, this is the way I did it

WITH exploded_array AS (
    SELECT 
      id AS movie_id,
      json_extract_path_text( JSON_EXTRACT_ARRAY_ELEMENT_TEXT(genres, seq.i) , 'id' ) AS id ,
      json_extract_path_text( JSON_EXTRACT_ARRAY_ELEMENT_TEXT(genres, seq.i) , 'name' ) AS name
    FROM movies_staging, seq_0_to_100 AS seq
  
    WHERE seq.i < JSON_ARRAY_LENGTH(genres)
  )
  
SELECT *
FROM exploded_array;
botchniaque
  • 4,698
  • 3
  • 35
  • 63
AdityaT101
  • 51
  • 1
  • 6