I have the following data in a matches table:
5;{\"Id\":1,\"Teams\":[{\"Name\":\"TeamA\",\"Players\":[{\"Name\":\"AAA\"},{\"Name\":\"BBB\"}]},{\"Name\":\"Tea
Shorter, faster and more elegant with a LATERAL join:
SELECT DISTINCT ON (t.team->>'Name') t.team
FROM matches m, json_array_elements(m.match->'Teams') t(team);
ORDER BY t.team->>'Name', m.id DESC; -- to get the "last"
If you just want distinct teams, the ORDER BY can go. Related:
There is no equality operator for the json data type in Postgres, but there is one for jsonb (Postgres 9.4+):