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
Related
I have a table with the next jsonb field -
{ "auth": [{"roles": ["role1", "role2"]}]}
When I do this query -
select jsonb_array_elements(role) as role from (
select x -> 'roles' as role
from test,
jsonb_array_elements(data->'auth') x
the output format is
role
----
"role1"
"role2"
The current problem is that if I try to add where it doesn't work
x.role = '"role1"', but this one does work x.role like '%"role1"%'
Thanks
It is possible to restore "role" field as text data using ->> operator.
create table test (data jsonb);
insert into test values('{ "auth": [{"roles": ["role1", "role2"]}]}');
with cte as (
select jsonb_array_elements(role) ->> 0 as role from
(
select x -> 'roles' as role
from test, jsonb_array_elements(data->'auth') x) y)
select role from cte where role = 'role1';
The problem is that jsonb_array_elements returns the results as jsonb, and you get in trouble comparing that to a string literal.
Use jsonb_array_elements_text instead of the first jsonb_array_elements call, then the results will be of type text, which should give you no trouble.
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.
I have column options with type jsonb , in format {"names": ["name1", "name2"]} which was created with
UPDATE table1 t1 SET options = (SELECT jsonb_build_object('names', names) FROM table2 t2 WHERE t2.id= t1.id)
and where names have type jsonb array.
SELECT jsonb_typeof(names) FROM table2 give array
Now I want to extract value of names as jsonb array. But query
SELECT jsonb_build_array(options->>'names') FROM table
gave me ["[\"name1\", \"name2\"]"], while I expect ["name1", "name2"]
How can I get value in right format?
The ->> operator will return the value of the field (in your case, a JSON array) as a properly escaped text. What you are looking for is the -> operator instead.
However, note that using the jsonb_build_array on that will return an array containing your original array, which is probably not what you want either; simply using options->'names' should get you what you want.
Actually, you don't need to use jsonb_build_array() function.
Use select options -> 'names' from table; This will fix your issue.
jsonb_build_array() is for generating the array from jsonb object. You are following wrong way. That's why you are getting string like this ["[\"name1\", \"name2\"]"].
Try to execute this sample SQL script:
select j->'names'
from (
select '{"names": ["name1", "name2"]}'::JSONB as j
) as a;
I have a field that is of type jsonb in a PostgreSQL database.
Example:
{"timestamp":"2016-12-14T04:15:04.836Z","receiptResult":{"status":"successful","timestamp":"2016-12-14T04:15:04.739Z","notes":"Customer Accepted"}}
How can I only return the "notes" in a select statement, I've tried:
SELECT data::json->>'notes' as notes
But nothing is returned, if I use:
SELECT data::json->'receiptResult' as notes;
It returns:
{"status":"successful","timestamp":"2016-114T04:15:04.739Z","notes":"Customer Accepted"}
But I only need the text after "notes".
Inside key receiptResult has another JSON object, you cannot access it in top-level. Try this:
WITH sample AS (
SELECT '{"timestamp":"2016-12-14T04:15:04.836Z","receiptResult":{"status":"successful","timestamp":"2016-12-14T04:15:04.739Z","notes":"Customer Accepted"}}'::jsonb AS my_column
)
SELECT my_column->'receiptResult'->>'notes' FROM sample;
As you can see, -> operator returns value as a JSONB and ->> operator returns as a text.
More info here.
If I have a jsonb column called value with fields such as:
{"id": "5e367554-bf4e-4057-8089-a3a43c9470c0",
"tags": ["principal", "reversal", "interest"],,, etc}
how would I find all the records containing given tags, e.g:
if given: ["reversal", "interest"]
it should find all records with either "reversal" or "interest" or both.
My experimentation got me to this abomination so far:
select value from account_balance_updated
where value #> '{}' :: jsonb and value->>'tags' LIKE '%"principal"%';
of course this is completely wrong and inefficient
Assuming you are using PG 9.4+, you can use the jsonb_array_elements() function:
SELECT DISTINCT abu.*
FROM account_balance_updated abu,
jsonb_array_elements(abu.value->'tags') t
WHERE t.value <# '["reversal", "interest"]'::jsonb;
As it turned out you can use cool jsonb operators described here:
https://www.postgresql.org/docs/9.5/static/functions-json.html
so original query doesn't have to change much:
select value from account_balance_updated
where value #> '{}' :: jsonb and value->'tags' ?| array['reversal', 'interest'];
in my case I also needed to escape the ? (??|) because I am using so called "prepared statement" where you pass query string and parameters to jdbc and question marks are like placeholders for params:
https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html