I am looking to extract multiple values from a jsonb column in Postgres, and am running into an issue where some values are coming back null.
Setting up schema:
create table jsonb_test (test jsonb);
insert into jsonb_test values('{
"title": "test",
"tags": [
{"tag": 1},
{"tag": 2}
]
}'::jsonb);
Query that I am running:
select jsonb_path_query(test, '$.title'::jsonpath) as title,
jsonb_path_query(test, '$.tags.tag'::jsonpath) as tag
from jsonb_test
The result that I am looking for should be:
| title | tag |
|-------|-----|
| test | 1 |
| test | 2 |
and what I am getting is:
| title | tag |
|-------|-----|
| test | 1 |
| null | 2 |
For the query, I have tried everything I can think of to make the null go away, selecting the second set of values from a cross join as opposed to the same table, aggregating it into a regular array and then using unnest, etc, but I can't seem to get it to work, and more importantly, I don't understand what is causing that second null.
What is the easiest way (performance is not expected to be an issue here) to get the result that I am looking for?
especially not on a fiddle site that requires to run scripts from multiple sites.
? I see db<>fiddle as a Godsend - different servers (even Firebird, for which I have a fondness - SQL in check constraints!) and other features (e.g. compare) - so what's the problem with scripts from multiple sites? I mean, don't most sites run scripts from multiple sites? I do agree with you though that code should be included in the answer as well as in a fiddle!