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
Related
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'
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');
Please see the table given below. The table contains the json string and need to create a json array with those json string. But When I use JSON_Query and For Json Path it adds additional header. (Alias name or the source column name). How to generate the json array without alias name or source column name.
Please see the example given below.
DECLARE #jsonTbl TABLE (id INT,json VARCHAR(MAX))
INSERT INTO #jsonTbl (id,json) VALUES (1,'{"id":"1A", "names":{"firstname":"Name1"}}')
INSERT INTO #jsonTbl (id,json) VALUES (1,'{"id":"2A", "names":{"firstname":"Name2"}}')
SELECT JSON_QUERY(json) AS 'someName'
FROM #jsonTbl
FOR JSON AUTO
--When I use the above select query it returns the data as
[{"SomeName":{"id":"1A", "names":{"firstname":"Name1"}}},{"SomeName":{"id":"2A", "names":
{"firstname":"Name2"}}}]
Formatted JSON
```[
{
"someName":{
"id":"1A",
"names":{"firstname":"Name1"}
}
},
{
"someName":{
"id":"1B",
"names":{
"firstname":"Name1"
}
}
}
]
--But need the result as follows. Do not need someName
[
{
"id":"1A",
"names":{
"firstname":"Name1"
}
},
{
"id":"2A",
"names":{
"firstname":"Name2"
}
}
]```
You can use OPENJSON() together with CROSS APPLY
SELECT j.[id], j.[names]
FROM #jsonTbl t
CROSS APPLY OPENJSON(t.json, '$') WITH ([id] VARCHAR(100),
[names] NVARCHAR(MAX) AS JSON) j
FOR JSON AUTO
Demo
How can I bulk insert JSON arrays into SQL Server 2016 tables with column separated without using JSON file (will get values with outer object from input)
Sample JSON can be found here
You can try [Not tested]:
[
{ "code" : 001,"name": "Prashant" },
{ "code" : 002,"name": "Steve" }
]
Query:
INSERT INTO your_table (code, name)
SELECT code,name
FROM OPENJSON(#json)
WITH (code int, name nvarchar(50))
To read from JSON object:
{"OUTER":{"ABC":"TEST_WB_New","XYZ":"9085"}} -- Your JSON
INSERT INTO your_table_name
SELECT ABC, XYZ
FROM OPENJSON(#json)
WITH (
ABC nvarchar(50) 'strict $.OUTER.ABC',
XYZ nvarchar(50) '$.OUTER.XYZ' AS JSON
)
I'm wondering how to ensure that the data inserted into a json or jsonb column is an object, not an array (or an array of objects).
Example:
-- ok
insert into users (settings) values ('{ "theme": "cobalt" }')
-- ok
insert into users (settings) values ('{}')
-- error!
insert into users (settings) values ('[]')
-- error!
insert into users (settings) values ('[{}]')
Thanks!
you could do smth like:
t=# create table so16(j jsonb check (left(ltrim(j::text), 1) <> '['));
CREATE TABLE
t=# insert into so16 values('{"b":[1,2,3]}');
INSERT 0 1
t=# insert into so16 values('[1,2,3]');
ERROR: new row for relation "so16" violates check constraint "so16_j_check"
DETAIL: Failing row contains ([1, 2, 3]).
t=# insert into so16 values(' [1,2,3]');
ERROR: new row for relation "so16" violates check constraint "so16_j_check"
DETAIL: Failing row contains ([1, 2, 3]).