You first need to normalize the data model to be able to properly join on the list of codes:
select a.id, a.name, x.*
from table_a a
left join lateral (
select b.code, b.name
from unnest(string_to_array(a.b_codes, ',')) as c(code)
join table_b b on b.code = c.code
) as x on true
;
This returns the following result:
id | name | code | name
---+------+------+-----
1 | abc | a | xx
1 | abc | b | yy
2 | def | |
The codes can be aggregated directly in the derived table (sub-query):
select a.id, a.name, coalesce(x.codes, '[]') as codes
from table_a a
left join lateral (
select jsonb_agg(jsonb_build_object('code', b.code, 'name', b.name)) as codes
from unnest(string_to_array(a.b_codes, ',')) as c(code)
join table_b b on b.code = c.code
) as x on true
;
The coalesce() is necessary to get an empty array for id = 2, otherwise the column codes from the derived table would be null.
This can now be converted into JSON values:
select jsonb_build_object('id', a.id, 'name', a.name, 'b_codes', coalesce(x.codes, '[]'))
from table_a a
left join lateral (
select jsonb_agg(jsonb_build_object('code', b.code, 'name', b.name)) as codes
from unnest(string_to_array(a.b_codes, ',')) as c(code)
join table_b b on b.code = c.code
) as x on true
;
Online example