Below is for BigQuery Standard SQL
#standardSQL
SELECT STRING_AGG(y, ', ' ORDER BY y) json
FROM (
SELECT STRING_AGG(TRIM(x) ORDER BY t DESC LIMIT 1) y
FROM `project.dataset.table`,
UNNEST(SPLIT(REGEXP_REPLACE(json, r'{|}', ''))) x
GROUP BY TRIM(SPLIT(x, ':')[OFFSET(0)])
)
Note: above solution generic and does not require knowing in advance attribute names (like a
, b
, etc.) rather it parse and extract whatever it will find. Obviously it relies on assumption of simple jsons like in your examples
You can test, play with above using sample data from your question as in example below
#standardSQL
WITH `project.dataset.table` AS (
SELECT '{"a":"1", "b":"2"}' json, 3 t UNION ALL
SELECT '{"b":"4"}', 2 UNION ALL
SELECT '{"a":"4", "c":"5"}', 1
)
SELECT STRING_AGG(y, ', ' ORDER BY y) json
FROM (
SELECT STRING_AGG(TRIM(x) ORDER BY t DESC LIMIT 1) y
FROM `project.dataset.table`,
UNNEST(SPLIT(REGEXP_REPLACE(json, r'{|}', ''))) x
GROUP BY TRIM(SPLIT(x, ':')[OFFSET(0)])
)
with result
Row json
1 "a":"1", "b":"2", "c":"5"
Because (as I mentioned) it is generic enough - you can add rows with more attributes w/o changing code like in below
#standardSQL
WITH `project.dataset.table` AS (
SELECT '{"a":"1", "b":"2"}' json, 3 t UNION ALL
SELECT '{"b":"4"}', 2 UNION ALL
SELECT '{"a":"4", "c":"5"}', 1 UNION ALL
SELECT '{"abc":"1", "xyz":"2"}', 3 UNION ALL
SELECT '{"abc":"3", "vwy":"4"}', 3
)
SELECT STRING_AGG(y, ', ' ORDER BY y) json
FROM (
SELECT STRING_AGG(TRIM(x) ORDER BY t DESC LIMIT 1) y
FROM `project.dataset.table`,
UNNEST(SPLIT(REGEXP_REPLACE(json, r'{|}', ''))) x
GROUP BY TRIM(SPLIT(x, ':')[OFFSET(0)])
)
with result
Row json
1 "a":"1", "abc":"1", "b":"2", "c":"5", "vwy":"4", "xyz":"2"