Skip to content
Advertisement

Redshift get Json Keys

I have a table field with a value like this:

I want to know if there is some way to get the “0fc8a2a1-e334-43b8-9311-ce46da9cd32c” and “4344d89b-7f0d-4453-b2c5-d0d4a39d7d25” values.

Advertisement

Answer

Redshift is not good with JSON, and especially not good with arbitrary JSON keys (as @GMB mentioned). Also it’s not good with nested data structures.

So actually, you have 2 problems:

  1. extacting the json keys. I see 2 options here:
  • Use python UDF
  • Use regex
  1. unnesting an array of keys into a table. There’s a trick to unnest data into rows (see CROSS JOIN with seq table in queries belowbelow) – described in this SO answer.

1. Solution with python UDF

you can implement json parsing in python and register it as a user defined function https://docs.aws.amazon.com/redshift/latest/dg/udf-python-language-support.html

function:

query:

2. Solution with REGEX magic

Redshift has some regex functions. That’s a working example that would do the job for the payload you specified:

result looks like:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement