Я хотел бы отформатировать вывод JSON
моего результирующего набора так, чтобы у меня было вложенное json
для всех строк на id
.
Когда я запускаю:
select array_to_json(array_agg(row_to_json(t)))
from (
select --id,
(
select array_to_json(array_agg(row_to_json(d)))
from (
select id, gpstime, lat, lon, heading, speed, acceleration, travelmode
from timestamped_segments
) d
) as ID_feild
from timestamped_segments limit 1
) t;
Я получаю:
"array_to_json"
"[
{""id_feild"":[{""id"":656001,""gpstime"":1459465600,""lat"":41.163172,""lon"":-8.5838214,""heading"":187,
""speed"":0.17,""acceleration"":7.95461130142212,""travelmode"":3},
{""id"":656001,""gpstime"":1459465601,""lat"":41.163237,""lon"":-8.58381,""heading"":62,""speed"":1.41,""acceleration"":-15.5309135317802,""travelmode"":3}
]
}
]"
Но я бы хотел отформатировать вывод следующим образом:
"array_to_json"
"[
{656001:[{gpstime"":1459465600,""lat"":41.163172,""lon"":-8.5838214,""heading"":187,
""speed"":0.17,""acceleration"":7.95461130142212,""travelmode"":3},
{gpstime"":1459465601,""lat"":41.163237,""lon"":-8.58381,""heading"":62,""speed"":1.41,""acceleration"":-15.5309135317802,""travelmode"":3}
]
},
{656002:[{gpstime"":1459465600,""lat"":41.163172,""lon"":-8.5838214,""heading"":187,
""speed"":0.17,""acceleration"":7.95461130142212,""travelmode"":3},
{gpstime"":1459465601,""lat"":41.163237,""lon"":-8.58381,""heading"":62,""speed"":1.41,""acceleration"":-15.5309135317802,""travelmode"":3}
]
},
{656003:[{.....}
]
}
]"
Как мне переписать мой запрос?