AWS Athena is a managed big data query system based on S3 and Presto. It supports a bunch of big data formats like JSON, CSV, Parquet, ION, etc. Schemas are applied at query time via AWS Glue.
To get started with Athena you define your Glue table in the Athena UI and start writing SQL queries. For my project I’ve been working on heavily nested JSON. The schema for this data is very involved so I opted to just use JSON path and skip defining my schema in AWS Glue. Of course that was after I wasted two days trying to figure out how to do it.
Setting up AWS Athena JSON tables
AWS Athena requires that your JSON objects be single line. That means you can’t have pretty printed JSON in your S3 bucket.
To use Athena and JSON Path for a nested JSON object like this. You simply define a single column called “data” with a string type and you are done.
Then you query using JSON Path like this.
Example of a simple JSON PATH Athena query
json_extract_scalar(data, '$.component1.date') as service_date,
json_extract_scalar(data, '$.component2.objects.type') as component2_json,
json_extract_scalar(data, '$["component3"]["objects"]["xy"]["xy-01"]') as xy_value
Note that the json_extract and json_extract_scalar functions take a string column as their argument.
One thing to watch out for is that the dash character “-” is banned from the first type of JSON Path. So you might have to use the bracket notation instead.
Using the UNNEST operator to pivot and flatten JSON arrays of objects.
Often when working with JSON data you will have arrays of objects. You might want to split each entry in a nested array into its own row. This can be accomplished with the UNNEST operator.
Here we have used json_extract to retrieve a nested JSON object. Then we cast it into a json array which the UNNEST operator accepts.
SELECT json_extract_scalar(data, '$.component1.date') as service_date,
json_extract_scalar(components, '$.date') as component_date,
json_extract_scalar(components, '$.type') as component_type,
CROSS JOIN UNNEST(CAST(json_extract(data, '$.component1.objects') as array(json))) as t (components)