Reading JSON array as one of the SQL table column - sql

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

Related

Select specific value from JSON list with SQL

Suppose I have a table column with person data organized as a JSON array, with categories and names.
In SQL, I can then easily select the data for a specific element in the array:
SELECT JSON_VALUE('{ "Persons": [{"PersonCat":"1","Name":"John"},{"PersonCat":"2","Name":"Henry"}]}','$.Persons[0].Name') AS SelectedPerson
I will then get "John".
But what if I want the person with "PersonCat" = 2? And null if PersonCat does not exist in the list?
I'm not sure it is possible to do this with the JSON_VALUE method, but it is possible using OPENJSON
DECLARE #json nvarchar(max)
SET #json = '{ "Persons": [{"PersonCat":"1","Name":"John"},{"PersonCat":"2","Name":"Henry"},{"Name":"Bob"}]}'
SELECT[PersonCat], [Name]
FROM OPENJSON(#json, '$.Persons')
WITH (
PersonCat NVARCHAR(512) '$.PersonCat',
[Name] NVARCHAR(512) '$.Name'
)
You can then add a WHERE clause to the SELECT to find the relevant PersonCat value (note: where the PersonCat property is missing from the array entry, the PersonCat column will be NULL)

How to get data from json column in SQL Server that starts with array element

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

JSON_VALUE value returning NULL instead of actual value

I have table in that one column contains jsonstring.While trying to read value from json result coming as NULL instead of name value.
query:
SELECT JSON_VALUE(value, '$.UUID') as uuid,
JSON_VALUE(value, '$.name') as name
FROM (select *
from openjson('[{
"UUID": "123",
"name": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApMAAACbCAYAAAA+2hC5AAAgAElEQVR4Xu2df+w821nXi1RBiq3F0h+IuNUmSJUUEBQTg1ujRiOhFwMIJIStaSFIgrdEI5hIP40m/PqDSyyICHbBYIEmcFE0oNUuGAkaEETEKCWslWoLLVVsKbTF6/O6zGPPd76zOz92ZnY+s6+TPJnZ2TPnx+ucnXnvc86Z+YCnGCQgAQlIQAISkIAEJDCQwAcMPM/TJCABCUhAAhKQgAQk8BTFpJ1AAhI4ReB3xhcvCvvbYW8Me6moJCABCUhAAnUCikn7hAQksA0ET4RtKvu42GJ8zvCW2HmeqCQgAQlIQAKKSfuABG6PAMLwT4ThaUQ4EnJ7jsZ/iy9/MuwlVaTPiu3rbg+fNZaABCQggXME9EzaPySwPgIIRcQj2zbR+B8izv+qEByqfQQkxnHOf0P1/cti+63rw2WNJCABCUjgEgKKyUvoea4ElkFgU4nHRyrxhwcyw/+uhGEKxGMhFNtKXwrJX4jIv6ftBL+XgAQkIIHbI6CYvL02t8bLJ4CIe0YYw9MExCICsRSJiMIXhL0vjPhl+L74gHh8vNoOqfEL46T/VJz4p2P/9UMS8hwJSEACElg3AcXkutvX2i2fAAIxh6QRj3Vh2KUGzG1EOB6qbZdz2uJ8d0T4zCrSV8f2y9pO8HsJSEACErhNAorJ22x3a30dApvI9vdWgpH9XDVdL80PxYFjzZpK/Jw4+F/C8EKOGUoheYiEWXjzS2NmYFoSkIAEJLAeAorJ9bSlNVkOAYTiF4Y9N4x9vI85ZF0vJV5FBBuCMLfXqsk2Mn5lGNsMPFtyf60Cma8EJCAB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goJmeAbBYSkIAEJCABCUhgrQQUk2ttWeslAQlIQAISkIAEZiCgmJwBsllIQAISkIAEJCCBtRJQTK61Za2XBCQgAQlIQAISmIGAYnIGyGYhAQlIQAISkIAE1kpAMbnWlrVeEpCABCQgAQlIYAYCiskZIJuFBCQgAQlIQAISWCsBxeRaW9Z6SUACEpCABCQggRkIKCZngGwWEpCABCQgAQlIYK0EFJNrbVnrJQEJSEACEpCABGYgoJicAbJZSEACEpCABCQggbUSUEyutWWtlwQkIAEJSEACEpiBgGJyBshmIQEJSEACEpCABNZKQDG51pa1XhKQgAQkIAEJSGAGAorJGSCbhQQkIAEJSEACElgrAcXkWlvWeklAAhKQgAQkIIEZCCgmZ4BsFhKQgAQkIAEJSGCtBBSTa21Z6yUBCUhAAhKQgARmIPD/APn2VQVBlmlnAAAAAElFTkSuQmCC"
}]')) ojson
Result:
Why and How to solve this?
Thanks.
I would personally suggest using OPENJSON with a WITH clause:
DECLARE #JSON nvarchar(MAX) = N'[{
"UUID": "123",
"name": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApMAAACbCAYAAAA+2hC5AAAgAElEQVR4Xu2df+w821nXi1RBiq3F0h+IuNUmSJUUEBQTg1ujRiOhFwMIJIStaSFIgrdEI5hIP40m/PqDSyyICHbBYIEmcFE0oNUuGAkaEETEKCWslWoLLVVsKbTF6/O6zGPPd76zOz92ZnY+s6+TPJnZ2TPnx+ucnXnvc86Z+YCnGCQgAQlIQAISkIAEJDCQwAcMPM/TJCABCUhAAhKQgAQk8BTFpJ1AAhI4ReB3xhcvCvvbYW8Me6moJCABCUhAAnUCikn7hAQksA0ET4RtKvu42GJ8zvCW2HmeqCQgAQlIQAKKSfuABG6PAMLwT4ThaUQ4EnJ7jsZ/iy9/MuwlVaTPiu3rbg+fNZaABCQggXME9EzaPySwPgIIRcQj2zbR+B8izv+qEByqfQQkxnHOf0P1/cti+63rw2WNJCABCUjgEgKKyUvoea4ElkFgU4nHRyrxhwcyw/+uhGEKxGMh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EJSEACEpCABGYgoJicAbJZSEACEpCABCQggbUSUEyutWWtlwQkIAEJSEACEpiBgGJyBshmIQEJSEACEpCABNZKQDG51pa1XhKQgAQkIAEJSGAGAorJGSCbhQQkIAEJSEACElgrAcXkWlvWeklAAhKQgAQkIIEZCCgmZ4BsFhKQgAQkIAEJSGCtBBSTa21Z6yUBCUhAAhKQgARmIKCYnAGyWUhAAhKQgAQkIIG1ElBMrrVlrZcEJCABCUhAAhKYgYBicgbIZiEBCUhAAhKQgATWSkAxudaWtV4SkIAEJCABCUhgBgKKyRkgm4UEJCABCUhAAhJYKwHF5Fpb1npJQAISkIAEJCCBGQgoJmeAbBYSkIAEJCABCUhgrQQUk2ttWeslAQlIQAISkIAEZiCgmJwBsllIQAISkIAEJCCBtRJQTK61Za2XBCQgAQlIQAISmIGAYnIGyGYhAQlIQAISkIAE1kpAMbnWlrVeEpCABCQgAQlIYAYCiskZIJuFBCQgAQlIQAISWCsBxeRaW9Z6SUACEpCABCQggRkIKCZngGwWEpCABCQgAQlIYK0EFJNrbVnrJQEJSEACEpCABGYgoJicAbJZSEACEpCABCQggbUSUEyutWWtlwQkIAEJSEACEpiBgGJyBshmIQEJSEACEpCABNZKQDG51pa1XhKQgAQkIAEJSGAGAorJGSCbhQQkIAEJSEACElgrAcXkWlvWeklAAhKQgAQkIIEZCCgmZ4BsFhKQgAQkIAEJSGCtBBSTa21Z6yUBCUhAAhKQgARmIPD/APn2VQVBlmlnAAAAAElFTkSuQmCC"
}]';
SELECT UUID,
[Name]
FROM OPENJSON(#JSON)
WITH (UUID int,
[name] nvarchar(MAX)) OJ;
As for why you got NULL, that's covered in the documentation:
Return value
Returns a single text value of type nvarchar(4000). The collation of the returned value is the same as the collation of the input expression.
If the value is greater than 4000 characters:
In lax mode, JSON_VALUE returns null.
In strict mode, JSON_VALUE returns an error.
If you have to return scalar values greater than 4000 characters, use OPENJSON instead of JSON_VALUE. For more info, see OPENJSON (Transact-SQL).
I've added emphasis to the relevant part. The value of [name] in your example is 14322 characters; well over 4000. As you can see, the documentation also recommends the method I used.

JSON_Query with For Json Path

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

SQL Server: Update table based on JSON

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;