0

I have a table in AWS Redshift with a column called json, which stores a nested JSON object as a string. I need to create a new table with three columns: sid, skill_name, and skill_vdd. The json column contains a nested JSON array under the key rows, and I want to extract the values from this array.

Here is a sample JSON in the json column:

{
    "Id": 888,
    "Command": "uuuu",
    "step": "rows": [
        {"sid": 1515, "skill_name": "jjjj", "skill_vdd": 9999},
        {"sid": 333, "skill_name": "ttt", "skill_vdd": 333}
    ]
}

I've tried using various redshift json functions, but I’m not able to flatten the json

How can I flatten this nested JSON and create a table with the following columns: sid, skill_name, skill_vdd from the json column in Redshift?

1 Answer 1

1

I'm going to assume that all entries for this column don't just have 2 values in their array "rows". In this case you will want to json_parse() this data into a SUPER data type and then "unnest" these array values. AWS has a good page on unnesting here - https://docs.aws.amazon.com/redshift/latest/dg/query-super.html

This question comes up a lot and I've answered it before here - UNNEST Super data in redshift

If you need help with getting this to work reach out again and show what code you have so far.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Bill, followed this page and it worked for me: medium.com/fishtown-analytics/…
if you have better solution please share here.
Better is relative. If what you have fully works and is fast enough then you are done, so it's better. However, Redshift has a built-in method for unnesting arrays that will be much less SQL, faster, and more supportable in the long-run. It just takes parsing your json into a super type and then following the process laid out in the AWS page.

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.