PosgreSQL: Convert JSON array to JSON object - postgresql

I need some help in converting JSON array in postgresTO JSON object as below. Please Note the keys are not known in advance, any key can show up in the JSON array. Is there a way to just flatten it out.
INPUT:-
[{"col1": "1", "col2": null, "col3": "7"}, {"col4": "19"}, {"col5": "19", "col6":"18"}]
OUTPUT:-
{"col1": "1", "col2": null, "col3": "7", "col4": "19", "col5": "19", "col6":"18"}
Thank you

First flatten (using json_array_elements and then json_each on each array element) and then aggregate back with json_object_agg.
select json_object_agg(k, v)
from json_array_elements
('[
{"col1":"1","col2":null,"col3":"7"},
{"col4":"19"},
{"col5":"19","col6":"18"}
]') as ja
cross join lateral json_each(ja) as jl(k, v);

Related

How to get a specific key from jsonb in postgresql?

I have column in jsonb named "lines" with many object like this :
[
{
"a" : "1",
"b" : "2",
"c" : "3"
},
{
"a" : "4",
"b" : "5",
"c" : "6"
}
]
This is my query
SELECT *
FROM public.test
WHERE public.test.lines::jsonb ? '[{"c"}]'
In my query i want to get only rows which contain the "c" key in this array
But i have nothing after execution
A quick solution:
SELECT
'c',
*
FROM
jsonb_path_query('[{"a": "1", "b": "2", "c": "3"}, {"a": "4", "b": "5", "c": "6"}]', '$[*].c');
?column? | jsonb_path_query
----------+------------------
c | "3"
c | "6"
The ? operator only works with strings, not with json objects. If you want to test if any of the array elements contains a key with the value c you can use a JSON path predicate:
SELECT *
FROM test
WHERE lines::jsonb ## '$[*].c != null'

How to get uniq data from jsonb column from some key which contains array?

I use PostgreSQL 11.9 and I have jsonb column in my table product with key SIZE and my goal get all unique available sizes in all products table, how query should look?
Right now I have this, but this is row for each product, I expected like each row equal some unique size gloablly for whole data base, all products
what I have
SELECT DISTINCT sub.SIZE FROM (
SELECT
jsonb_build_object('SIZE', p.extras->'SIZE') AS SIZE
FROM products AS p
WHERE p.extras IS NOT NULL
) sub
WHERE sub.SIZE != '{"SIZE": null}';
and my result
{"SIZE": ["32", "34", "36", "38", "52", "54", "48", "50", "40", "42", "44", "46", "32/34,36/38,52/54,48/50,40/42,44/46"]}
{"SIZE": ["38", "40", "42", "46", "48", "50", "52", "38,40,42,46,48,50,52"]}
{"SIZE": ["270", "150", "270x150cm"]}
{"SIZE": ["30", "33", "30-33"]}
{"SIZE": ["13", "5", "32", "US13.5 / EU32"]}
{"SIZE": ["3", "3 years"]}
{"SIZE": ["25", "5", "40", "25.5 / EU40"]}
{"SIZE": ["6", "23", "US6 / EU23"]}
this exapmle contain SIZE 38 in separate row result, regarding that this is dupcate, whould be result with uniq data. I expected result like this
["38", "40" .....]
or
"38"
"40"
...
this is example extra column from product row. extra column has type jsonb
{"SIZE": ["3", "5", "3.5"], "COLOUR": "Vit", "CONDITION": "new", "OWN_COLOUR": "Vit"}
SIZE key this is array
UPDATED
RESOLVED
Thank you everyone, ritgh now two approach for resolver this, with the same result 1154 rows in result, bu in one query present distinct and in another distinct not present, and the same result, but across all products a lot of duplicate sizes, how it's possible, jsonb_array_elements generated uniq result by default ?
select distinct x.size
from products p
cross join lateral jsonb_array_elements_text(p.extras -> 'SIZE') as x(size)
select jsonb_array_elements(p.extras -> 'SIZE') as type
from products AS p
group by type
I think you just want:
select distinct extras ->> 'SIZE' size
from products
You can implement a filtering logic in the where clause if needed. Maybe:
select distinct extras ->> 'SIZE' size
from products
where extras ->> 'SIZE' is not null
On the other hand, if json attribute SIZE is an array, then we need to unnest it first:
select distinct x.size
from products p
cross join lateral jsonb_array_elements_text(t.extras -> 'SIZE') as x(size)

Querying an array of JSONB fields in PostgresQL

I have trouble figuring out how to query PostgreSQL JsonB datatype. I have a simple table with the structure:
CREATE TABLE myTable (id BIGINT PRIMARY KEY, answers JSONB)
All Json documents in the column "answers" are of the form:
[
{"R" : "aaa", "V" : 25},
{"R" : "aaa", "V" : 31},
{"R" : "bbb", "V" : 38}
...
]
There can be many elements in the list, but all elements will have a "R" and a "V" item.
I would like to retrieve a table using SQL, listing Ids, and a Json list of all the "V"s where "R" == "aaa" .
For the example above, I would get:
Id for the record
[25, 31] -- the two "V" values where "R" == "aaa"
Any thoughts? Any help appreciated I have spent some time on the JSon paths examples available on the web, but haven't found something similar.
Thanks in advance.
Note: Postgresql 12+ only
Using jsonpath:
WITH data(id, json_arr) AS (
VALUES (1, $$[
{ "R": "aaa", "V": 25 },
{ "R": "aaa", "V": 31 },
{ "R": "bbb", "V": 38 }
]$$::JSONB)
)
SELECT id,
-- $[*] : "inside the top level array"
-- ? (#.R == "aaa") : "keep only when the "R" key's value is "aaa""
-- .V : "select the "V" key of those elements"
jsonb_path_query_array(json_arr, '$[*] ? (#.R == "aaa").V')
FROM data
returns:
+--+----------------------+
|id|jsonb_path_query_array|
+--+----------------------+
|1 |[25, 31] |
+--+----------------------+
Note: you can also use
jsonb_path_query_array(
json_arr,
'$[*] ? (#.R == $r_value).V',
'{"r_value": "aaa"}' -- pass the 'r_value' to the query above
)
I would expand the elements out, filter by R, and then reaggregate. This results in an int[] array.
select m.id, array_agg((a.obj->>'V')::int) as vvals
from mytable m
cross join lateral jsonb_array_elements(answers) as a(obj)
where a.obj->>'R' = 'aaa';
all in pure JSONB:
SELECT id, jsonb_agg(ans->'V') FROM (
SELECT id, jsonb_array_elements(answers) AS ans
FROM myTable
) zz
WHERE ans->'R' = '"aaa"'
GROUP BY id;

Postgres Return jsonb values as jsonb array?

tl;dr -- is there some way to get values as a jsonb_array from a jsonb object in postgres?
Trying to use recursive cte in postgres to flatten an arbitrarily deep tree structure like this:
{
"type": "foo",
"properties": {...},
"children": [
"type": "bar",
"properties": {...},
"children": [
"type": "multivariate",
"variants": {
"arbitrary-name": {
properties: {...},
children: [...],
},
"some-other-name": {
properties: {...},
children: [...],
},
"another": {
properties: {...},
children: [...],
}
}
]
]
}
Generally following this post, however I'm stuck at processing the type: "multivariate" node where what I really want is essentially jsonb_agg(jsonb_object_values(json_object -> 'variants'))
Update:
Sorry, I obviously should have included the query I tried:
WITH RECURSIVE tree_nodes (id, json_element) AS (
-- non recursive term
SELECT
id, node
FROM trees
UNION
-- recursive term
SELECT
id,
CASE
WHEN jsonb_typeof(json_element) = 'array'
THEN jsonb_array_elements(json_element)
WHEN jsonb_exists(json_element, 'children')
THEN jsonb_array_elements(json_element -> 'children')
WHEN jsonb_exists(json_element, 'variants')
THEN (select jsonb_agg(element.value) from jsonb_each(json_element -> 'variants') as element)
END AS json_element
FROM
tree_nodes
WHERE
jsonb_typeof(json_element) = 'array' OR jsonb_typeof(json_element) = 'object'
)
select * from tree_nodes;
The schema is just an id & a jsonb node column
This query gives an error:
ERROR: set-returning functions are not allowed in CASE
LINE 16: THEN jsonb_array_elements(json_element -> 'children')
^
HINT: You might be able to move the set-returning function into a LATERAL FROM item.
I just want Object.values(json_element -> 'variants') 😫
Update 2:
After reading this all again, I realized this is a problem due to me using a recent version of PostgreSQL (10.3), which apparently no longer allows returning a set from a CASE statement, which was kind of the crux of getting this tree-flattening approach to work afaict. There's probably some way to achieve the same thing in recent versions of PostgreSQL but I have no idea how I'd go about doing it.
Use jsonb_each() in the FROM clause together with jsonb_agg(<jsonb_each_alias>.value) in the SELECT, for example:
select
id,
jsonb_agg(child.value)
from
(values
(101, '{"child":{"a":1,"b":2}}'::jsonb),
(102, '{"child":{"c":3,"d":4,"e":5}}'::jsonb
)) as t(id, json_object), -- example table, replace values block with actual tablespec
jsonb_each(t.json_object->'child') as child
group by t.id
You can always chain other jsonb functions which return setof jsonb (e.g. jsonb_array_elements) in the FROM if you need to iterate the higher level arrays before the jsonb_each; for example:
select
id,
jsonb_agg(sets.value)
from
(values
(101, '{"children":[{"a_key":{"a":1}},{"a_key":{"b":2}}]}'::jsonb),
(102, '{"children":[{"a_key":{"c":3,"d":4,"e":5}},{"a_key":{"f":6}}]}'::jsonb
)) as t(id, json_object), -- example table, replace values block with actual tablespec
jsonb_array_elements(t.json_object->'children') elem,
jsonb_each(elem->'a_key') as sets
group by t.id;
Update Answer
In answer to your comment and question edit about needing to walk the 'children' of each tree node and extract the 'variants'; I would achieve this by splitting the CTE into multiple stages:
with recursive
-- Constant table for demonstration purposes only; remove this and replace below references to "objects" with table name
objects(id, object) as (values
(101, '{"children":[{"children":[{"variants":{"aa":11}},{"variants":{"ab":12}}],"variants":{"a":1}},{"variants":{"b":2}}]}'::jsonb),
(102, '{"children":[{"children":[{"variants":{"cc":33,"cd":34,"ce":35}},{"variants":{"f":36}}],"variants":{"c":3,"d":4,"e":5}},{"variants":{"f":6}}]}'::jsonb)
),
tree_nodes as ( -- Flatten the tree by walking all 'children' and creating a separate record for each root
-- non-recursive term: get root element
select
o.id, o.object as value
from
objects o
union all
-- recursive term - get JSON object node for each child
select
n.id,
e.value
from
tree_nodes n,
jsonb_array_elements(n.value->'children') e
where
jsonb_typeof(n.value->'children') = 'array'
),
variants as (
select
n.id,
v.value
from
tree_nodes n,
jsonb_each(n.value->'variants') v -- expand variants
where
jsonb_typeof(n.value->'variants') = 'object'
)
select
id,
jsonb_agg(value)
from
variants
group by
id
;
This ability of breaking a query up into a "pipeline" of operations is one of my favourite things about CTEs - it makes the query much easier to understand, maintain and debug.
db<>fiddle
Expanded the test data with more children elements and deeper structure (more nested elements):
{
"type": "foo",
"children": [
{
"type" : "bar1",
"children" : [{
"type" : "blubb",
"children" : [{
"type" : "multivariate",
"variants" : {
"blubb_variant1": {
"properties" : {
"blubb_v1_a" : 100
},
"children" : ["z", "y"]
},
"blubb_variant2": {
"properties" : {
"blubb_v2_a" : 300,
"blubb_v2_b" : 4200
},
"children" : []
}
}
}]
}]
},
{
"type" : "bar2",
"children" : [{
"type" : "multivariate",
"variants" : {
"multivariate_variant1": {
"properties" : {
"multivariate_v1_a" : 1,
"multivariate_v1_b" : 2
},
"children" : [1,2,3]
},
"multivariate_variant2": {
"properties" : {
"multivariate_v2_a" : 3,
"multivariate_v2_b" : 42,
"multivariate_v2_d" : "fgh"
},
"children" : [4,5,6]
},
"multivariate_variant3": {
"properties" : {
"multivariate_v3_a" : "abc",
"multivariate_v3_b" : "def"
},
"children" : [7,8,9]
}
}
},
{
"type" : "blah",
"variants" : {
"blah_variant1": {
"properties" : {
"blah_v1_a" : 1,
"blah_v1_b" : 2
},
"children" : [{
"type" : "blah_sub1",
"variants" : {
"blah_sub1_variant1" : {
"properties" : {
"blah_s1_v1_a" : 12345,
"children" : ["a",1, "bn"]
}
}
}
}]
},
"blah_variant2": {
"properties" : {
"blah_v2_a" : 3,
"blah_v2_b" : 42,
"blah_v2_c" : "fgh"
},
"children" : [4,5,6]
}
}
}]
}
]
}
Result:
variants json
----------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
"multivariate_variant1" {"children": [1, 2, 3], "properties": {"multivariate_v1_a": 1, "multivariate_v1_b": 2}}
"multivariate_variant2" {"children": [4, 5, 6], "properties": {"multivariate_v2_a": 3, "multivariate_v2_b": 42, "multivariate_v2_d": "fgh"}}
"multivariate_variant3" {"children": [7, 8, 9], "properties": {"multivariate_v3_a": "abc", "multivariate_v3_b": "def"}}
"blah_variant1" {"children": [{"type": "blah_sub1", "variants": {"blah_sub1_variant1": {"properties": {"children": ["a", 1, "bn"], "blah_s1_v1_a": 12345}}}}], "properties": {"blah_v1_a": 1, "blah_v1_b": 2}}
"blah_variant2" {"children": [4, 5, 6], "properties": {"blah_v2_a": 3, "blah_v2_b": 42, "blah_v2_c": "fgh"}}
"blubb_variant1" {"children": ["z", "y"], "properties": {"blubb_v1_a": 100}}
"blubb_variant2" {"children": [], "properties": {"blubb_v2_a": 300, "blubb_v2_b": 4200}}
"blah_sub1_variant1" {"properties": {"children": ["a", 1, "bn"], "blah_s1_v1_a": 12345}}
The Query:
WITH RECURSIVE json_cte(variants, json) AS (
SELECT NULL::jsonb, json FROM (
SELECT '{/*FOR TEST DATA SEE ABOVE*/}'::jsonb as json
)s
UNION
SELECT
row_to_json(v)::jsonb -> 'key', -- D
CASE WHEN v IS NOT NULL THEN row_to_json(v)::jsonb -> 'value' ELSE c END -- C
FROM json_cte
LEFT JOIN LATERAL jsonb_array_elements(json -> 'children') as c ON TRUE -- A
LEFT JOIN LATERAL jsonb_each(json -> 'variants') as v ON TRUE -- B
)
SELECT * FROM json_cte WHERE variants IS NOT NULL
The WITH RECURSIVE structure checks elements in a recursive ways. The first UNION part is the starting point. The second part is the recursive part where the last calculation is taken for the next step.
A: if in the current JSON a children element exists all elements will be expanded into one row per child
B: if the current JSON has an element variants all elements will be expanded into a record. Note that in the example one JSON element can either contain a variants or a children element.
C: if there is a variants element then the expanded record will be converted back into a json. The resulting structure is {"key" : "name_of_variant", "value" : "json_of_variant"}. The values will be the JSON for the next recursion (the JSON of the variants can have own children elements. That's why it works). Otherwise the expanded children elements will be the next data
D: if there is a variants element then the key is printed

Modify datatype of a field in JSONB in Postgres

I have a JSONB column with JSON that looks like this:
{"id" : "3", "username" : "abcdef"}
Is there way to update the JSON to :
{"id" : 3, "username" : "abcdef"}
Update the jsonb column using the concatenate operator ||, e.g.:
create table example(id int, js jsonb);
insert into example values
(1, '{"id": "3", "username": "abcdef"}');
update example
set js = js || jsonb_build_object('id', (js->>'id')::int)
returning *;
id | js
----+---------------------------------
1 | {"id": 3, "username": "abcdef"}
(1 row)
select json_build_object('id',CAST(j->>'id' AS NUMERIC),'username',j->>'username')::jsonb from (
select '{"id" : "3", "username" : "abcdef"}' :: jsonb AS j
)t