Course contentsShow
← Databases & SQL
Lesson 335 of 1,9534. Advanced Data AggregationFree lesson

Aggregation of JSON Metadata

Extract and aggregate key-value metrics embedded within semi-structured columns.

In this lesson, you will learn how to extract specific metrics hidden inside semi-structured JSON columns and perform mathematical aggregations on that data.

The Problem of "Hidden" Data

Modern databases often store flexible, object-based data in JSON columns. While this is great for storing variable metadata (like product features or sensor configurations), the data is technically just a string or an object type to the database engine. You cannot simply perform SUM() or AVG() on a column if the values you need are buried inside keys like {"temperature": 22.5, "humidity": 45}.

Extracting and Calculating

To aggregate this data, you must perform a two-step process: Extraction and Aggregation. First, you use a database-specific extraction function (such as ->> in PostgreSQL or JSON_VALUE in SQL Server) to pull the specific key’s value out of the JSON blob. Once the value is "unpacked" into a standard numeric or string format, you can treat it like any other column for your GROUP BY and aggregate functions.

Consider a table sensor_logs containing a metadata column:

SELECT 
    device_id, 
    AVG(CAST(metadata->>'temperature' AS FLOAT)) AS avg_temp
FROM sensor_logs
WHERE metadata->>'status' = 'active'
GROUP BY device_id;

Why This Matters

In this example, the database engine treats the JSON content as an opaque blob until we explicitly request the temperature key. By casting the extracted text value into a FLOAT, we enable the AVG function to calculate the mean of those specific readings across our groups.

This technique is essential for building dashboards from semi-structured logs where you might not know every key in advance, but you know the specific metrics you need to monitor. Remember that because you are performing extractions on every row, large tables can become slow. Always ensure your queries include a WHERE clause to filter the JSON data before the aggregation starts, which keeps your compute costs manageable.

Key Takeaway: To aggregate JSON metadata, first extract the value using key-access functions and cast it to a numeric type, then apply standard aggregation functions as you would with any native column.