1

I don't find a solution to query an array of json object with redshift. For each row, I have an array of json object stored in a column as the following:

[{'a':x,'b':y,'type':z},{'a':x,'b':y,'type':w},{'a':x,'b':y,'type':z},{a:x,b:y,type:z}]

For each row, I want to extract the number of 'type' z object in a new column. Anyone would have an idea?

Thanks a lot,

Nicolas

1

2 Answers 2

2

I've used this syntax to loop through json arrays in redshift fields.

CREATE TEMP TABLE seq
(i int); INSERT INTO seq VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8);

SELECT distinct 
json_extract_path_text(json_extract_array_element_text(yourfieldname, seq.i),'type') as type
FROM yourtablename, seq AS seq
--- arman why is this less than the array
WHERE seq.i < JSON_ARRAY_LENGTH(yourfieldname)

;

DROP TABLE seq;
Sign up to request clarification or add additional context in comments.

Comments

0

I've found more recent information on this. You can now use the following syntax according to the AWS Redshift documentation:

CREATE TABLE bar AS SELECT json_parse('{"scalar_array": [1, 2.3, 45000000]}') AS data;
SELECT index, element FROM bar AS b, b.data.scalar_array AS element AT index;

https://docs.aws.amazon.com/redshift/latest/dg/query-super.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.