PostgreSQL: Query elements in a JSON array - sql

I have a database table like the following:
---------------------------------------
| id | json_array (jsonb) |
---------------------------------------
| 1 | [{"a": 1}, {"a": 5}, {"a": 1}] |
| 2 | [{"a": 2}] |
| 3 | [{"a": 1}] |
---------------------------------------
I want to use PostgreSQL's JSON query abilities to select certain sub dictionaries in the json_array array, e.g. dictionaries where "a": 1.
The output should be
------------------
| id | json_dict |
------------------
| 1 | {"a": 1} |
| 1 | {"a": 1} |
| 3 | {"a": 1} |
------------------
The following query works for the first element in each array, but I want to check for all of them:
SELECT id, json_array -> 0
FROM my_table
WHERE json_array -> 0 -> "a" = 1;

Assuming this is a JSONB column, you can use the #> operator with a json object:
select *
from my_table
where json_array #> '[{"a": 1}]';
Online example: https://rextester.com/SACUDU51598
If you want all objects as rows, you need to unnest the array:
select t.id, e.obj
from data t
cross join jsonb_array_elements(json_array) as e(obj)
where e.obj = '{"a": 1}'
Online example: https://rextester.com/DLG27322

demo:db<>fiddle
You can expand your array elements into one element each row with jsonb_array_elements(). This can be filtered:
SELECT
id,
elems.value
FROM
mytable,
jsonb_array_elements(data) elems
WHERE
elems.value = '{"a":1}'

Related

Postgres: How to join table with values from jsonb[] column

I have two tables as follows
accounts
------------------------------------------
| ID | LOCATIONS |
|------------------------------------------|
| 1 | [{ "id" : 1}, { "id" : 3 }] |
|------------------------------------------|
| 2 | [] |
------------------------------------------
regions
----------------------------
| ID | DATA |
|---------------------------|
| 1 | {"name": "South"} |
|---------------------------|
| 2 | {"name": "West"} |
|---------------------------|
| 3 | {"name": "North"} |
|---------------------------|
| 4 | {"name": "East"} |
---------------------------
locations is of type jsonb[]
Now I wanted to get result as follows
------
| NAME |
|------|
| South|
|------|
| North|
------
Please help with the postgresql query to get this.
Edited for jsonb[] type:
Demo
select
r.data ->> 'name' as name
from
accounts a
cross join unnest(a.locations) al
inner join regions r on r.id = (al ->> 'id')::int
P.S: for jsonb type:
You can use jsonb_to_recordset function and CROSS JOIN to join JSON array record with table.
Demo
select
r.data ->> 'name' as name
from
accounts a
cross join jsonb_to_recordset(a.locations) as al(id int)
inner join regions r on r.id = al.id
One option would be using JSONB_ARRAY_ELEMENTS() along with cross joins such as
SELECT r.data->>'name' AS "Name"
FROM accounts AS a,
regions AS r,
JSONB_ARRAY_ELEMENTS(a.locations) AS l
WHERE (value->>'id')::INT = r.id
Demo
PS. if the data type of locations is JSON rather than JSONB, then just replace the current function with JSON_ARRAY_ELEMENTS()

Getting the index of the JSON array using SQL in Snowflake

I'm flattening a JSON data in snowflake using the Lateral Flatten.
I have the JSON data as follows:
{
"Fruits": [
{
"Apple_Type" : Type_A,
"Banana_Type": Type_B
},
{
"Apple_Type" : Type_A2,
"Banana_Type": Type_B3
}
]
}
I used the following query to get the flattened data
SELECT v.value:Apple_Type,
v.value:Banana_Type
FROM Table1, LATERAL FLATTEN(input => Fruits) v
My Result:
--------------------------------
| Apple_Type | Banana_Type |
--------------------------------
| Type_A | Type_B |
| Type_A2 | Type_B3 |
--------------------------------
How do I get the index of the data. I want the table as follows
----------------------------------------------
| Apple_Type | Banana_Type | Index |
----------------------------------------------
| Type_A | Type_B | 0 | -> Because Apple_Type is from index 0 in the Fruit Array
| Type_A2 | Type_B3 | 1 | -> Because Banana_Type is from index 1 in the Fruit Array
----------------------------------------------
Using INDEX:
INDEX
The index of the element, if it is an array; otherwise NULL.
SELECT v.value:Apple_Type,
v.value:Banana_Type,
v.index
FROM Table1, LATERAL FLATTEN(input => Fruits) v

Postgresql: select rows by OR condition - including going through json array

I have a table created by following query:
create table data
(
id integer not null unique,
owner text,
users jsonb not null
);
The table looks like this:
+----+-------+---------------------------------------------+
| id | owner | users |
+----+-------+---------------------------------------------+
| 1 | alice | [] |
| 2 | bob | [{"accountId": "alice", "role": "manager"}] |
| 3 | john | [{"accounId": "bob", "role": "guest"}] |
+----+-------+---------------------------------------------+
I need to get rows 1 and 2 on behalf of Alice.
Getting owner-based rows works perfect:
SELECT *
FROM data
WHERE owner = 'alice'
Getting jsonb-based rows is a little trickier though managable:
SELECT *
FROM data, jsonb_array_elements(users) x
WHERE (x ->> 'accountId') = 'alice'
But getting them together gets me just the jsonb-based ones:
SELECT *
FROM data, jsonb_array_elements(users) x
WHERE owner = 'alice' OR (x ->> 'accountId') = 'alice'
How do I get the selection that looks like following?
+----+-------+---------------------------------------------+
| id | owner | users |
+----+-------+---------------------------------------------+
| 1 | alice | [] |
| 2 | bob | [{"accountId": "alice", "role": "manager"}] |
+----+-------+---------------------------------------------+
Even better if I can get a selection that looks like this
+----+----------+
| id | role |
+----+----------+
| 1 | owner |
| 2 | manager |
+----+----------+
The problem is with the empty json array, which evicts the corresponding row from the result set when cross joined with jsonb_array_elements(). Instead, you can make a left join lateral:
select d.*
from data d
left join lateral jsonb_array_elements(d.users) as x(js) on 1 = 1
where 'alice' in (d.owner, x.js ->> 'accountId')
Note that, if your array always contains 0 or 1 element, tyou don't need the lateral join - your query would be simpler phrased as:
select d.*
from data d
where 'alice' in (d.owner, d.data -> 0 ->> 'accountId')
Demo on DB Fiddle - both queries return:
id | owner | users
-: | :---- | :------------------------------------------
1 | alice | []
2 | bob | [{"role": "manager", "accountId": "alice"}]

In Hive, how to combine multiple tables to produce single row containing array of objects?

I have two tables as follows:
users table
==========================
| user_id name age |
|=========================
| 1 pete 20 |
| 2 sam 21 |
| 3 nash 22 |
==========================
hobbies table
======================================
| user_id hobby time_spent |
|=====================================
| 1 football 2 |
| 1 running 1 |
| 1 basketball 3 |
======================================
First question: I would like to make a single Hive query that can return rows in this format:
{ "user_id":1, "name":"pete", "hobbies":[ {hobby: "football", "time_spent": 2}, {"hobby": "running", "time_spent": 1}, {"hobby": "basketball", "time_spent": 3} ] }
Second question: If the hobbies table were to be as follows:
========================================
| user_id hobby scores |
|=======================================
| 1 football 2,3,1 |
| 1 running 1,1,2,5 |
| 1 basketball 3,6,7 |
========================================
Would it be possible to get the row output where scores is a list in the output as shown below:
{ "user_id":1, "name":"pete", "hobbies":[ {hobby: "football", "scores": [2, 3, 1]}, {"hobby": "running", "scores": [1, 1, 2, 5]}, {"hobby": "basketball", "scores": [3, 6, 7]} ] }
I was able to find the answer to my first question
select u.user_id, u.name,
collect_list(
str_to_map(
concat_ws(",", array(
concat("hobby:", h.hobby),
concat("time_spent:", h.time_spent)
))
)
) as hobbies
from users as u
join hobbies as h on u.user_id=h.user_id
group by u.user_id, u.name;

Can I sum an array of jsonb in Postgresql with dynamic keys in a select statement?

I have a jsonb object in postgres:
[{"a": 1, "b":5}, {"a":2, "c":3}]
I would like to get an aggregate sum per unique key:
{"a":3, "b":5, "c":3}
The keys are unpredictable.
Is it possible to do this in Postgres with a select statement?
Query:
SELECT key, SUM(value::INTEGER)
FROM (
SELECT (JSONB_EACH_TEXT(j)).*
FROM JSONB_ARRAY_ELEMENTS('[{"a": 1, "b":5}, {"a":2, "c":3}]') j
) j
GROUP BY key
ORDER BY key;
Results:
| key | sum |
| --- | --- |
| a | 3 |
| b | 5 |
| c | 3 |
DB Fiddle