ATHENA JSON blob with multple columns of the same name

0

Hi, I am trying to add the below into its own column each:

[{"name":"Conscious","id":"181310000019395590"},{"name":"Legal 500","id":"181310000002802041"}]

This is one cell in my table. I have previously been using the command

[select *, json_extract_scalar(tag, '$.name') as Tag_name, json_extract_scalar(tag, '$.id') as Tag_id FROM "database";]

However this is not returning anything as there are multple values called 'name'. I am looking to add columns to my table which will look like:

Enter image description here

Or even better would be

Enter image description here

Any help is much appreciated :) Thanks

asked a year ago773 views
1 Answer
0

Hello,

Thanks for reaching out.

The json_extract_scalar function does not work with arrays as it only returns scalar values such as boolean and strings, hence when you are passing your array tag, the query is returning blank. Please refer to our documentation [1] to learn more about querying JSON in Athena.

If you want the output to have a single row with concatenated tag names and IDs, you can try the following query:

WITH tag_elements AS (
  SELECT 
    tag,
    CAST(json_extract(tag, concat('$[', CAST(i AS VARCHAR), ']')) AS JSON) AS element
  FROM 
    "database"
  CROSS JOIN 
    UNNEST(sequence(0, json_array_length(tag) - 1)) AS t(i)
)
SELECT 
  array_join(
    array_agg(json_extract_scalar(element, '$.name')),
    ', '
  ) AS Tag,
  array_join(
    array_agg(json_extract_scalar(element, '$.id')),
    ', '
  ) AS Tag_id
FROM 
  tag_elements;

This query uses json_extract function along with concat to extract each element from the JSON array - tag. We generate a sequence of numbers from 0 to the length of the JSON array minus 1 using sequence, and then cross join it to the original table. We then use json_extract_scalar to extract the "name" and "id" values from each element and concatenate them using array_agg and array_join, which returns the desired output.

Hope this information was helpful.

References:

[1] Extracting data from JSON - https://docs.aws.amazon.com/athena/latest/ug/extracting-data-from-JSON.html

AWS
SUPPORT ENGINEER
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions