Select value of jsonb column in PostgreSQL
The shortest version would be:
SELECT DISTINCT value->'Tag' AS tag
FROM Documents, jsonb_array_elements(Documents.Tags);
The jsonb_array_elements()
function unnests the JSONB array into a set of rows with a single column called "value". It uses an implicit "lateral join" on the Documents
table.
This gives you the distinct tags as jsonb
values. If you want them as a text
value, use the ->>
operator instead of ->
.
you can also use the following snippet.
SELECT
id,
data::json->'name' as name
FROM books;