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
Related
I have a database table that I need to extract data from where the column of interest has json it. What makes this particularly difficult is the most outer elements of the json is '[' & ']' as in the parent element is an array. I need to get the value associated with key 'Name' (which in this case is 'MS220'). However, I'm not able to path correctly to the key I want.
The below JData table is a duplicate copy of the data I need to perform the extract on. Between SELECT OPENJSON, JSON_VALUE, JSON_QUERY etc., how can I retrieve the value I'm looking for?
Below is a couple of selects I've tried but not quite getting it.
CREATE TABLE JData
(
JsonData nvarchar(max)
)
INSERT INTO JData (JsonData)
VALUES
('[
{
"Categories": [
{
"QuerySourceNames": [
"QAsset"
],
"Id": "eceae85a-ffc6-49f4-8f6a-78ce2b4b274e",
"Name": "emsdba"
}
],
"Id": "525b4f07-0f67-43ac-8070-a0e6c1ceb1b9",
"Name": "MS220"
}
]')
SELECT *
FROM OPENJSON (JData,'$.[0]')
WITH (
[Name] varchar(10) '$.Name'
)
SELECT
JSON_VALUE(JData,'$') as v
#AaronBertrand: I had to modify the answer a little since the table also has a column labeled [name] as well. Is there a way to UPDATE ParamName to a new value?
SELECT
t.[Name],
ParamName
FROM
[myDB].[dbo].[myTable] t
CROSS APPLY
OPENJSON (t.params)
WITH
(
Categories nvarchar(max) AS json,
Id uniqueidentifier,
ParamName varchar(10) '$.Name'
);
SELECT Name FROM dbo.JData
CROSS APPLY OPENJSON (JsonData)
WITH
(
Categories nvarchar(max) AS json,
Id uniqueidentifier,
[Name] varchar(10)
);
Example db<>fiddle
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
)
I need to insert json from a nested json file into a column in Oracle. For example, in the following json
{
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
I need to store the whole json:
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
in a db column. How can i do that? I am using Oracle DB.
I have tried the following query but its not working. (Says clob isn't a valid datatype)
select x.*
from json_tab t,
json_table (t.json_data, '$.[*]'
COLUMNS
name VARCHAR2(4000) PATH '$.name',
cars clob PATH '$.cars[*]') x;
I have tried the same using varchar2 datatype but it selects null.
Assuming you use oracle 12c, try to experiment with this query to achieve the result you need.
select x.*, json_object(key 'cars' value x.cars format json) cars_json
from json_tab t,
json_table(t.json_data, '$'
COLUMNS
name VARCHAR2(4000) PATH '$.name',
cars VARCHAR(4000) format json PATH '$.cars[*]') as x;
I am trying to read a json array into a table, one of the nodes (Languages) in the array is an array in itself, and I am getting null for this particular column (Languages).
Below is the sample json:
DECLARE #json NVARCHAR(MAX) = '[
{
"Id":1,
"Name":"Test1",
"Languages":["L1", "L2"]
},
{
"Id":2,
"Name":"Test2",
"Languages":["L3", "L4"]
},
{
"Id":3,
"Name":"Test2",
"Languages":["L5", "L6"]
}]'
Below is the query I am using:
SELECT Id
, Name
, Languages
FROM OPENJSON(#json)
WITH (Id INT '$.Id'
, Name VARCHAR(20) '$.Name'
, Languages VARCHAR(200) '$.Languages')
Below is the current result:
However I need the result as below
What am I doing wrong? Please help.
You can use NVARCHAR(max) as json for Language item inside WITH clause.
From Microsoft docs (all the details can be found here):
If you don't specify AS JSON for a column, the function returns a
scalar value (for example, int, string, true, false) from the
specified JSON property on the specified path. If the path represents
an object or an array, and the property can't be found at the
specified path, the function returns null in lax mode or returns an
error in strict mode. This behavior is similar to the behavior of the
JSON_VALUE function.
So your query should look like this:
SELECT Id
, Name
, Languages
FROM OPENJSON(#json)
WITH (Id INT '$.Id'
, Name VARCHAR(20) '$.Name'
, Languages NVARCHAR(max) as json)
Results:
I hope maybe this query will be help you. Result is little bit different from you want.
DECLARE #json NVARCHAR(MAX) = '{"UserLang":[
{
"Id":1,
"Name":"Test1",
"Languages":["L1", "L2"]
},
{
"Id":2,
"Name":"Test2",
"Languages":["L3", "L4"]
},
{
"Id":3,
"Name":"Test2",
"Languages":["L5", "L6"]
}]}'
SELECT
JSON_VALUE(d.value,'$.Id') AS Id,
JSON_VALUE(d.value,'$.Name') AS Languages,
l.value AS Name
FROM OPENJSON(#json,'$.UserLang') AS d CROSS APPLY OPENJSON (d.value,'$.Languages') AS l