How to insert paranthesis,brackets,colon,double quotes inside column in postgresql - sql

I am working on application and i need to store { [ " : ] } these characters inside column.
How can i do it?

None of those character require special attention:
create table t (col text);
insert into t
values ('{ [ " : ] }');
However, if you are planning to stor (valid) JSON values in that column, it's better to declare it as jsonb rather than text:
create table t (col jsonb);
insert into t
values ('{"key1": 42, "key2": [1,2,3]}');
The only character that needs special attention in string literals in SQL is the single quote - which is escaped by doubling it, e.g.: 'Arthur''s house'

Related

Parse Json Response and insert into oracle

I am new to oracle and facing a little challenge in iterating through a json response.
I know there are many examples online which work on json array element and then insert, but i tried various things and couldnt get this to work.
The json response is like this:
{
'data':
{
'Key_one' : 'value_one',
'Key_two' : 'value_two'
}
}
I have a stored proc:
CREATE OR REPLACE PROCEDURE TEST(JSON_TEXT_DATA) AS
BEGIN
---- need a for loop here to dynamically iterate and insert.
INSERT INTO TABLE_NAME('KEY_ONE') values(json_value(JSON_TEXT_DATA,'$.data.Key_One'));
INSERT INTO TABLE_NAME('KEY_TWO') values(json_value(JSON_TEXT_DATA,'$.data.Key_Two'));
END;
You can try using JSON_TABLE to insert the values into your table. Using this method will put both keys into a single row in your table.
INSERT INTO table_name (key_one, key_two)
SELECT key1, key2
FROM JSON_TABLE ('{
"data":
{
"Key_one" : "value_one",
"Key_two" : "value_two"
}
}',
'$.data'
COLUMNS key1 VARCHAR2 PATH '$.Key_one', key2 VARCHAR2 PATH '$.Key_two');

how to save value in oracle using nq'[]' operator

I need to save string with single quote in column with nvarchar2 data type. I find here
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm
that I can save it using quote operator. I try to do it in my SQL query
insert into table (question_pattern_id, text, event_selection_id, context_theme_id, placeholder_body)
values ('65', nq'[👽]', '', '', nq'[👽]')
but it inserts ?? instead of 👽
Before data was added by query
insert into table (question_pattern_id, text, event_selection_id, context_theme_id, placeholder_body)
values ('65', n'👽', '', '', n'👽')
and everything was saved correctly

why insert statement not working in postgres trying to insert json?

Why my insert statement not working?
here is my code
http://sqlfiddle.com/#!17
create table json_check (
id serial primary key not null ,
body jsonb
)
insert into json_check (body)
values ('{
"test":"naveeb",
"data":"{'a':'ss'}"}')
It is showing me a syntax error.
It seems that your JSON is invalid.
insert into json_check (body)
values ('{
"test": "naveeb",
"data": {
"a": "ss"
}
}');
You can check your JSON status on https://jsonlint.com/.
Running example pasted on below link:
http://sqlfiddle.com/#!17/80ca3/6

All Values in json response including numeric values need to be in double quotes

Iam trying to retrieve the data from the table in json format with below query in SQL Server ,
Select * from TableA for json path, root( 'table 1')
Iam getting json response as ,
{
"TableA":[
{
"Id":34562,
"A Id":2,
"Name":"ZDR"
}
]
}
Where Field "ID" & "A Id" are declared as "int"in the database
Now i need those values should also be displayed within double quotes , is there any way that we can handle in SQL server 2016 itself, to make clear all the field values irrespective of datatype should be enclosed in double quotes please do need full !!!
Like above single query iam having set of 12 quires in a stored procedure eventually when i run the stored procedure i will get a json response where all the values should be enclosed within double quotes .
There is no "automagic" way to do this. What you can do is in your SELECT statements CAST your numeric columns to literal (varchar or nvarchar):
Select CAST(Id as nvarchar(10)) AS Id,
CAST([A Id], nvarchar(10)) AS "A ID",
[Name]
from TableA for json path, root( 'table 1');
Hope this helps!

Postgresql how to escape comma

I want to insert a string like
"keyId"=>"5cfd1f5bd6d0c2e6aae4421553a8b3ea","server"=>"http://10.10.27.10/keys/key_file.key","keyRotationPeriod"=>"10"
to the database, my current SQL is
insert abr.drm(id, name, type, info, ksid, iv)
0, "test", 1, ""keyId"=>"5cfd1f5bd6d0c2e6aae4421553a8b3ea","server"=>"http://10.10.27.10/keys/key_file.key","keyRotationPeriod"=>"10"", NULL, NULL
But I got error as
INSERT has more expressions than target columns,
LINE 1: ....10/keys/key_file.key','keyRotationPeriod"=>"10"',NULL,NULL)...
^
`
how to escape comma in postgresql?