I am try to get result like combine multiple column as single column in result but not got success.
how to get exepected result?
query:
select SchemaName,([create],[read],[update],[delete]) as [Permissions] from ApplicationRoles for json path;
Exepected Result:
{
"SchemaName": "TestSchema",
"Permissions": {
"create": true,
"read": true,
"update": true,
"delete": true
}
}
Thanks in advance.
Put the values you want in an additional array in a subquery:
CREATE TABLE dbo.YourTable(SchemaName sysname,
[create] bit,
[read] bit)
INSERT INTO dbo.YourTable
VALUES ('TestSchema',1,1);
GO
SELECT YT.SchemaName,
(SELECT YT.[create],
YT.[read]
FOR JSON PATH) AS Permissions --No WITHOUT_ARRAY_WRAPPER as this will result in "Permissions": "{\"create\":true,\"read\":true}"
FROM dbo.YourTable YT
FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER;
GO
DROP TABLE dbo.YourTable;
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 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
)
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
I am trying to update rows in table based on JSON I have. JSON has the following structure:
"sensors": [
{
"id": "5afd7160f16819f11814f6e2",
"num": 0,
"name": "AC01",
"enabled": true,
"unit": "Volt AC Phase 1",
"desc": "NAMsdafE",
"lt_disaster": 1,
"gt_disaster": 1,
"lt_high": 1,
"gt_high": 1,
"lt_average": 1,
"gt_average": 1
},...
Table dbo.sensors has same structure + few more columns. To insert such JSON object, not array, into table, I would do it this way:
INSERT INTO dbo.sensors (.......)
SELECT .......
FROM OPENJSON(#json)
WITH (
id varchar(200),
....
);
So I have 2 questions: how to iterate over each element in JSON array and update each row with the same id. Any help would be appreciated:)
1) once you change the json into a select statement, you can iterate over that using cursor.
2) you can treat json select statement as a table. That said, you can do insert, update, delete operations exactly as you do with two tables. For updated case you can use code like this:
With Json_data as
( SELECT .......
FROM OPENJSON(#json)
WITH (
id varchar(200),
....
)
update S set ....
from dbo.sensors as S
inner join Json_data as JD on JD.id = S.id
First, read documentation OPENJSON. This feature is available starting version 2016.
Next, apply new knowledge.
--truncated for shortness
--note: wrap JSON string in curly brackets {}
declare #json nvarchar(max)='{"sensors":[
{
"id": "5afd7160f16819f11814f6e2",
"num": 0,
"name": "AC01",
"more": "unused"
},
{ "id": "5afd7160f16819f11814f6e3",
"num": 0,
"name": "AC02"
}]}
'
--insert...
select * from
openjson(#json,'$.sensors') --note the "path" argument here
with(
id varchar(200),
num int,
name varchar(10)
) json --alias sometimes required.
You can use result (rowset) as it is a table.
;With Json_data as
( SELECT
Evaluation_IDNO,
Rating_IDNO,
Notes_TEXT,
NextSteps_TEXT,
EvaluationCategory_CODE,
EvalType_ID
FROM OPENJSON(#As_EvaluationCategory_Json) WITH
(
Evaluation_IDNO INT N'$.matrixId',
Rating_IDNO VARCHAR(150) N'$.ratingValue',
Notes_TEXT VARCHAR(MAX) N'$.notesText',
NextSteps_TEXT VARCHAR(MAX) N'$.nextStepsText',
EvaluationCategory_CODE VARCHAR(50) N'$.ratingData',
EvalType_ID VARCHAR(4) N'$.evalTypeId'
)
AS EvaluationCategories
)
UPDATE EvaluationRatings_T1 SET
UserCreatedBy_ID=#As_SignedOnWorker_ID,
User_ID=#Ls_User_Id,
WorkspaceCreatedBy_ID=#Ls_WorkspaceCreatedBy_Id,
BeginValidity_DTTM=#Ls_Evaluation_DTTM,
EndValidity_DTTM=#Ld_HighValidity_DTTM,
TransactionEvenSeq_NUMB=#An_TransactionEventSeq_NUMB,
Update_DTTM=#Ld_BeginValiditiy_DTTM,
WorkspaceUpdatedBy_ID=#Ls_WorkspaceUpdatedBy_ID,
Evaluation_IDNO=c1.Evaluation_IDNO,
Rating_IDNO=c1.Rating_IDNO,
Notes_TEXT=c1.Notes_TEXT,
NextSteps_TEXT=c1.NextSteps_TEXT,
EvaluationCategory_CODE=c1.EvaluationCategory_CODE,
EvalType_ID=c1.EvalType_ID
FROM Json_data c1
inner JOIN EvaluationRatings_T1 e1 on e1.Evaluation_IDNO=c1.Evaluation_IDNO
WHERE e1.Evaluation_IDNO=#AS_Evaluation_IDNO;