Skip to content
Advertisement

Splitting a nested dict-like varchar column into multiple columns using SQL presto

In my table I’ve a column, which is a varchar but has a nested dictionary-like format (three nested levels). Some entries have multiple key-value pairs (customer ID & name), while some just have a single entry (customer ID). For example:

I need a query that will break out the the column into a table like this:

I know a solution to extract for a single nested key-value pair, but cant edit it to work for a nested dictionary like this. I’m using Prestosql.

Advertisement

Answer

Common approach with this kind of dynamic json is to cast it to MAP, in this case nested map – MAP(VARCHAR, MAP(VARCHAR, JSON)) and use unnest to flatten the result:

Output:

customer_type location customer_id name
customer_type1 location1 12345 John
customer_type1 location2 12346 Conor
customer_type2 location3 12347 Brian
customer_type2 location4 12348
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement