Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
Last active November 11, 2023 10:57
Show Gist options
  • Save nilsmagnus/8302c63c0252101ce3e5d479a493c124 to your computer and use it in GitHub Desktop.
Save nilsmagnus/8302c63c0252101ce3e5d479a493c124 to your computer and use it in GitHub Desktop.
postgres med json
insert into artikler(article_type, content)
values (1, '{"articleId": "xyz123", "meta":{"tag":"bil", "title":"ny elbil", "codes":["a","b"]}}'::jsonb);
select content::text
from artikler;
select content -> 'meta' ->> 'title' as tittel
from artikler
where content -> 'meta' ->> 'tag' = 'bil';
select id, id, jsonb_array_elements(content -> 'meta' -> 'codes')
from artikler
where content @> '{"meta":{"codes":["a"]}}';
create index article_tag_idx on artikler using gin ((content -> 'meta' -> 'tag'));
docker start jsonbtesting || docker run -e POSTGRES_USER=jsonb_user -e POSTGRES_PASSWORD=dingafu -e POSTGRES_DB=jsonbtesting --name jsonbtesting -dt -p 5432:5432 postgres:15.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment