Get json value in pl/pgsql - json

I have json string like '{"ww":11}'
How can i get value 11 in pl/pgsql ?
I try:
json_var := '{"ww":11}'::json
json_var->ww;
json_var->"ww";
json_var.ww;
All of this failed.
Please help!
Dmitry

The json type is just text. The advantage of using it is syntax check. Just that.
Check this question

you need to typecast it to Json in order to do that.
select '{"ww":11}'::json->'ww' as ww_value
returns ....
11

Related

postgresql jsonpath support

I am running postgresql 12 and am trying to use jsonpath.
I'd like to use jsonb_path_query_first to get the length of an array. I know I can do json_array_length but would rather not.
Here is the query that I am trying to make work.
select jsonb_path_query_first('{"a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb, '$.s.length()'::jsonpath);
ERROR: syntax error, unexpected '(', expecting end of file at or near "(" of jsonpath input
LINE 1: ...a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb, '$.s.lengt...
Does the version of jsonpath in postgresql 12 not support this type of jsonpath functionality.
You can use the .size() method for this:
test# select
jsonb_path_query_first(
'{"a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb,
'$.s.size()'::jsonpath
);
jsonb_path_query_first
════════════════════════
5
(1 row)

Query on JSON response returns, Parse exception Unexpected end of input

Good day,
Hope you are well.
I am trying to use the Vincere API, and trying to query the response to only return where private_job:0. I am using Postman to test the API.
When I use the below request, doing my best to follow the instructions on the Documentation:
https://domain.vincere.io/api/v2/job/search/fl=job_title,private_job;sort=published_date asc?q=private_job:0
I get the following response:
"Parse exception Unexpected end of input, expected term_char, ws0, term or term_end (line 1, pos 14):\nprivate_job:0\n ^\n"
If I remove ?q=private_job:0, I get a valid response.
I am clearly doing something wrong. Please assist.
in query parameter the key name is q ,
q=private_job:0
but in documentation it says instead of q it should be fq
https://domain.vincere.io/api/v2/job/search/fl=job_title,private_job;sort=published_date asc?fq=private_job:0
Also if you are using special character q=private_job:0 # , then give the value in the query parameter session of postman it will url encode it automatically for you
This stumped me as well, turns out my issue was twofold.
Firstly, this error refers to their URL parser expecting to see the end character %23, so your query string needs to end with that.
Secondly, I was attempting to query the job_type and using the actual string value ie. job_type:PERMANENT%23. This actually needs to be the enum value (1 in this case).

How to put empty string in JSON_OBJECT_T object?

I am trying to add an empty string to a JSON_OBJECT_T using the following code but I am getting null in value and not empty string.
DECLARE
V_OBJ JSON_OBJECT_T;
BEGIN
V_OBJ := JSON_OBJECT_T();
V_OBJ.PUT('customerAccRef','');
DBMS_OUTPUT.PUT_LINE(V_OBJ.stringify);
END;
When I do this, I am getting the following json
{"customerAccRef":null}
and I want output as below
{"customerAccRef":""}
Can someone suggest what change I need to do to pass an empty string?
The reason it is happening is due to the fact that Oracle internally changes empty string to NULL values. It is due to some legacy reason and you may read this answer to know the history.
I couldn't find anywhere in the JSON documentation with an option to bypass this particular problem myself, although I'd be glad if someone could find it.
As a workaround to your problem, you could use TRIM function to convert a single space to blank string.
V_OBJ.PUT('customerAccRef' , TRIM(' '));
which gives
{"customerAccRef":""}
This seems to work both in Oracle 12.2 version; I tested in my local machine and in Oracle
18c : DEMO, as well as in 19c (LiveSQL online)
A point to also note here is that a simple select TRIM(' ') from dual always returns NULL, which is surprising and luckily for you, it works as expected with JSONs
In 19c, using put(..., '') you get an empty string.
Using put_null, or put(..., to_char(null)), put(..., <unitializedString>) you get the "real" null value.
DECLARE
V_OBJ JSON_OBJECT_T;
V_STRING VARCHAR2(20);
BEGIN
V_OBJ := JSON_OBJECT_T();
V_OBJ.PUT('doubleSingleQuotes','');
V_OBJ.PUT('toCharNull',to_char(null));
V_OBJ.PUT('uninitializedStringVariable',v_string);
V_OBJ.PUT_null('null');
V_OBJ.PUT('trimSpace', trim(' '));
DBMS_OUTPUT.PUT_LINE(V_OBJ.stringify);
END;
... gives :
{"doubleSingleQuotes":"","toCharNull":null,"uninitializedStringVariable":null,"null":null,"trimSpace":""}

Using a field atrribute in mysql parameter function cast

I want to use a variable (or an attribute of the table) as CAST parameter (AS), I cant find the proper syntax online.
The proof of concept looks like this:
SELECT
prd_template_field_type,
#type := prd_template_field_type,
Min(CAST(prd_template_field_rule_value AS #type)) AS Min,
Max(CAST(prd_template_field_rule_value AS #type)) AS Max
FROM tbl_prd_template_field
Please dont downvote, I really try to give my best to formulate this as clear as possible + long research has been done.
Thanks!

How to use if condition in SOQL?

How to reproduce the following statement of MySQL in SOQL:
SELECT IF(1 = 2,'true','false');
I am trying to do the following:
select if(field1=null,false,true) as res from table
But it is showing me unknown parsing error.
That is not possible in SOQL.
Not sure of the requirement here but you may use a formula field instead.