For anyone (like me) who is not quite able to visualize this, here is an example:
SELECT json_agg(trips)
FROM (
SELECT
json_agg(
json_build_object(
'recorded_at', created_at,
'latitude', latitude,
'longitude', longitude
)
) as trips
FROM data_tracks
GROUP by trip_log_id
)s
From StackOverflow user S-man https://stackoverflow.com/a/53087015