I want to dynamically concat strings in a jsonb array in my psql query which is as follows :
with cte as (select generate_series(1, 3) as num) select '["name", "number"||cte.num]'::jsonb as res from cte;
But I am getting this error :
ERROR: invalid input syntax for type json
LINE 1: ...e as (select generate_series(1, 5) as num) select '["name", ...
^
DETAIL: Token "|" is invalid.
CONTEXT: JSON data, line 1: ["name", "number"|...
So, here I am generating numbers from 1 to 3 and I want to append those numbers with "number" string in my jsonb array. This is what I want in my result:
res
----------------------
["name", "number1"]
["name", "number2"]
["name", "number3"]
Please tell me how do I get this. Thanks.
Something like this should work.
SELECT ( '["name", "number'|| num ||'"]' )::jsonb as res
FROM generate_series(1, 3) num
You were adding the pipes as characters after an element in a list. You should first build the array string (in parenthesis), and then convert it to jsonb.
Related
Consider a table like so:
CREATE TABLE items (
e uuid,
v jsonb
)
I insert the following values:
INSERT INTO items (e, v) VALUES
('9a70439e-33c0-4b34-91f5-efac20b58301', '"92cb730c-8b4f-46ef-9925-4fab953694c6"'),
('92cb730c-8b4f-46ef-9925-4fab953694c6', '"Bob"'),
('92cb730c-8b4f-46ef-9925-4fab953694c6', '52');
Note how for v, I have actually stringified text and numbers -- not an object.
Now, what if I wanted to write a query like so:
WITH match AS (
SELECT * FROM items WHERE e = '9a70439e-33c0-4b34-91f5-efac20b58301'
) SELECT * FROM items JOIN match ON match.v = items.e;
I would get:
Query Error: error: operator does not exist: jsonb = uuid
Is there a way I could tell postgres to "parse" the jsonb value, and see if it is a uuid?
I know about Postgres cast to UUID from JSON , where the ->> operator was able to do the trick. But I can't do that in this case, because our json value is a strong not an object.
You can use ->> 0 to extract the value as text:
SELECT * FROM items
WHERE e = '9a70439e-33c0-4b34-91f5-efac20b58301'
AND e::text = v ->> 0;
I fail to find any information on how to do in / subsetof / contains queries using JsonPath in Postgres.
e.g.
Assuming the following data in a jsonb column named data
{
"name": "foo",
"somearray" : [1,2,3,4,5]
}
Then I want to query this using something like
SELECT *
FROM mytable
where jsonb_path_exists(data, '($.somearray ??????? [2,4,6,8] ');
This does work:
SELECT *
FROM mytable
where jsonb_path_exists(data, '($ ? (#.somearray[*] == 2 || #.somearray[*] == 4 /*etc*/) ');
But I am hoping that there is some shorter syntax to do a proper subset test
Unfortunately no jsonpath array operators, but you can still use the array operators :
SELECT *
FROM mytable
where (data->>'somearray') :: integer[] #> [2,4,6,8] ;
i have the following table schema:
i want to convert the items column into a json string but the output of TO_JSON_STRING isn't what i need.
when i run this query i get:
SELECT id, store, TO_JSON_STRING(items) AS items_json
FROM nested_array_example
but what i need is items_json to be:
{"table": "3", "lamp": "7", "swedish_thing": "729"}
is there a way to do that in bigquery?
here is the query to generate the table's data:
INSERT INTO `project_name.data_seT_name.nested_array_example` (store, items, id)
VALUES ("ikea", [("table","3"),("lamp","7"),("swedish_thing",'729'),("swedish_thing_made_in_china",'5723')], '1')
Consider below approach
select id, store,
( select '{' || string_agg(format('"%s": "%s"', name, value)) || '}'
from t.items
) items_json
from `project_name.data_seT_name.nested_array_example` t
if applied to sample data from your question - output is
I am running a query on Postgresql 9.4.4 and am having trouble accessing data from a JSONB member which has spaces in the name.
When I run the following query...
"SELECT result FROM test"
I get the following result
{"test": 1, "ch0 gain": 2, "ch1 gain": 3}
When I run try to specify a member directly with spaces such as...
"SELECT result->'ch0 gain' FROM test"
The result is empty, but not the case if I run the following query...
"SELECT result->'test' FROM test"
Do I have the wrong syntax, or is it just not supported? I'm having trouble finding the answer.
You have to use the right Operator :
Operator Operand
-------------------
-> int
-> text
->> int
->> text
#> text[]
#>> text[]
Source: here
Modified :
CREATE TABLE justjson ( doc JSONB)
INSERT INTO justjson VALUES ('{"test": 1, "ch0 gain": 2, "ch1 gain": 3}');
select doc->'ch0 gain' from justjson
Output :
2
I am trying to combine rows and concatenate two columns (name, vorname) in a Postgres query.
This works good like this:
SELECT nummer,
array_to_string(array_agg(name|| ', ' ||vorname), '\n') as name
FROM (
SELECT DISTINCT
nummer, name, vorname
FROM myTable
) AS m
GROUP BY nummer
ORDER BY nummer;
Unfortunately, if "vorname" is empty I get no results although name has a value.
Is it possible get this working:
array_to_string(array_agg(name|| ', ' ||vorname), '\n') as name
also if one column is empty?
Use coalesce to convert NULL values to something that you can concatenate:
array_to_string(array_agg(name|| ', ' ||coalesce(vorname, '<missing>')), '\n')
Also, you can concatenate strings directly without collecting them to an array by using the string_agg function.
If you have 9.1, then you can use third parameter for array_to_string - null string
array_to_string(array_agg(name), ',', '<missing>') from bbb