在SqlServer中增加Json处理的方法
Sql Server 存储非结构话数据可以使用xml类型,使用xpath方式查询,以前写过一篇随笔:Sql Server xml 类型字段的增删改查
除了xml类型也可以使用文本类型(char、vchar等)存储json格式的数据,如何在sql语句中解析json数据,这里有一篇博客 [转]在SqlServer 中解析JSON数据,它的来源是Consuming JSON Strings in SQL Server
针对json解析需要一个自定义类型Hierarchy、一个表值函数parseJSON、一个标量值函数ToJSON。语句如下:
/****** Object: UserDefinedTableType [dbo].[Hierarchy] Script Date: 2016/5/6 17:24:48 ******/
CREATE TYPE [dbo].[Hierarchy] AS TABLE(
[element_id] [INT] NOT NULL,
[sequenceNo] [INT] NULL,
[parent_ID] [INT] NULL,
[Object_ID] [INT] NULL,
[NAME] [NVARCHAR]() NULL,
[StringValue] [NVARCHAR](MAX) NOT NULL,
[ValueType] [VARCHAR]() NOT NULL,
PRIMARY KEY CLUSTERED
(
[element_id] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO
Hierarchy
CREATE FUNCTION [dbo].[parseJSON]( @JSON NVARCHAR(MAX))
RETURNS @hierarchy TABLE
(
element_id INT IDENTITY(, ) NOT NULL, /* internal surrogate primary key gives the order of parsing and the list order */
sequenceNo [int] NULL, /* the place in the sequence for the element */
parent_ID INT,/* if the element has a parent then it is in this column. The document is the ultimate parent, so you can get the structure from recursing from the document */
Object_ID INT,/* each list or object has an object id. This ties all elements to a parent. Lists are treated as objects here */
NAME NVARCHAR(),/* the name of the object */
StringValue NVARCHAR(MAX) NOT NULL,/*the string representation of the value of the element. */
ValueType VARCHAR() NOT null /* the declared type of the value represented as a string in StringValue*/
)
AS
BEGIN
DECLARE
@FirstObject INT, --the index of the first open bracket found in the JSON string
@OpenDelimiter INT,--the index of the next open bracket found in the JSON string
@NextOpenDelimiter INT,--the index of subsequent open bracket found in the JSON string
@NextCloseDelimiter INT,--the index of subsequent close bracket found in the JSON string
@Type NVARCHAR(),--whether it denotes an object or an array
@NextCloseDelimiterChar CHAR(),--either a '}' or a ']'
@Contents NVARCHAR(MAX), --the unparsed contents of the bracketed expression
@Start INT, --index of the start of the token that you are parsing
@end INT,--index of the end of the token that you are parsing
@param INT,--the parameter at the end of the next Object/Array token
@EndOfName INT,--the index of the start of the parameter at end of Object/Array token
@token NVARCHAR(),--either a string or object
@value NVARCHAR(MAX), -- the value as a string
@SequenceNo int, -- the sequence number within a list
@name NVARCHAR(), --the name as a string
@parent_ID INT,--the next parent ID to allocate
@lenJSON INT,--the current length of the JSON String
@characters NCHAR(),--used to convert hex to decimal
@result BIGINT,--the value of the hex symbol being parsed
@index SMALLINT,--used for parsing the hex value
@Escape INT --the index of the next escape character DECLARE @Strings TABLE /* in this temporary table we keep all strings, even the names of the elements, since they are 'escaped' in a different way, and may contain, unescaped, brackets denoting objects or lists. These are replaced in the JSON string by tokens representing the string */
(
String_ID INT IDENTITY(, ),
StringValue NVARCHAR(MAX)
)
SELECT--initialise the characters to convert hex to ascii
@characters='0123456789abcdefghijklmnopqrstuvwxyz',
@SequenceNo=, --set the sequence no. to something sensible.
/* firstly we process all strings. This is done because [{} and ] aren't escaped in strings, which complicates an iterative parse. */
@parent_ID=;
WHILE = --forever until there is nothing more to do
BEGIN
SELECT
@start=PATINDEX('%[^a-zA-Z]["]%', @json collate SQL_Latin1_General_CP850_Bin);--next delimited string
IF @start= BREAK --no more so drop through the WHILE loop
IF SUBSTRING(@json, @start+, )='"'
BEGIN --Delimited Name
SET @start=@Start+;
SET @end=PATINDEX('%[^\]["]%', RIGHT(@json, LEN(@json+'|')-@start) collate SQL_Latin1_General_CP850_Bin);
END
IF @end= --no end delimiter to last string
BREAK --no more
SELECT @token=SUBSTRING(@json, @start+, @end-)
--now put in the escaped control characters
SELECT @token=REPLACE(@token, FROMString, TOString)
FROM
(SELECT
'\"' AS FromString, '"' AS ToString
UNION ALL SELECT '\\', '\'
UNION ALL SELECT '\/', '/'
UNION ALL SELECT '\b', CHAR()
UNION ALL SELECT '\f', CHAR()
UNION ALL SELECT '\n', CHAR()
UNION ALL SELECT '\r', CHAR()
UNION ALL SELECT '\t', CHAR()
) substitutions
SELECT @result=, @escape=
--Begin to take out any hex escape codes
WHILE @escape>
BEGIN
SELECT @index=,
--find the next hex escape sequence
@escape=PATINDEX('%\x[0-9a-f][0-9a-f][0-9a-f][0-9a-f]%', @token collate SQL_Latin1_General_CP850_Bin)
IF @escape> --if there is one
BEGIN
WHILE @index< --there are always four digits to a \x sequence
BEGIN
SELECT --determine its value
@result=@result+POWER(, @index)
*(CHARINDEX(SUBSTRING(@token, @escape++-@index, ),
@characters)-), @index=@index+ ; END
-- and replace the hex sequence by its unicode value
SELECT @token=STUFF(@token, @escape, , NCHAR(@result))
END
END
--now store the string away
INSERT INTO @Strings (StringValue) SELECT @token
-- and replace the string with a token
SELECT @JSON=STUFF(@json, @start, @end+,
'@string'+CONVERT(NVARCHAR(), @@identity))
END
-- all strings are now removed. Now we find the first leaf.
WHILE = --forever until there is nothing more to do
BEGIN SELECT @parent_ID=@parent_ID+
--find the first object or list by looking for the open bracket
SELECT @FirstObject=PATINDEX('%[{[[]%', @json collate SQL_Latin1_General_CP850_Bin)--object or array
IF @FirstObject = BREAK
IF (SUBSTRING(@json, @FirstObject, )='{')
SELECT @NextCloseDelimiterChar='}', @type='object'
ELSE
SELECT @NextCloseDelimiterChar=']', @type='array'
SELECT @OpenDelimiter=@firstObject WHILE = --find the innermost object or list...
BEGIN
SELECT
@lenJSON=LEN(@JSON+'|')-
--find the matching close-delimiter proceeding after the open-delimiter
SELECT
@NextCloseDelimiter=CHARINDEX(@NextCloseDelimiterChar, @json,
@OpenDelimiter+)
--is there an intervening open-delimiter of either type
SELECT @NextOpenDelimiter=PATINDEX('%[{[[]%',
RIGHT(@json, @lenJSON-@OpenDelimiter)COLLATE SQL_Latin1_General_CP850_Bin)--object
IF @NextOpenDelimiter=
BREAK
SELECT @NextOpenDelimiter=@NextOpenDelimiter+@OpenDelimiter
IF @NextCloseDelimiter<@NextOpenDelimiter
BREAK
IF SUBSTRING(@json, @NextOpenDelimiter, )='{'
SELECT @NextCloseDelimiterChar='}', @type='object'
ELSE
SELECT @NextCloseDelimiterChar=']', @type='array'
SELECT @OpenDelimiter=@NextOpenDelimiter
END
---and parse out the list or name/value pairs
SELECT
@contents=SUBSTRING(@json, @OpenDelimiter+,
@NextCloseDelimiter-@OpenDelimiter-)
SELECT
@JSON=STUFF(@json, @OpenDelimiter,
@NextCloseDelimiter-@OpenDelimiter+,
'@'+@type+CONVERT(NVARCHAR(), @parent_ID))
WHILE (PATINDEX('%[A-Za-z0-9@+.e]%', @contents COLLATE SQL_Latin1_General_CP850_Bin))<>
BEGIN
IF @Type='Object' --it will be a -n list containing a string followed by a string, number,boolean, or null
BEGIN
SELECT
@SequenceNo=,@end=CHARINDEX(':', ' '+@contents)--if there is anything, it will be a string-based name.
SELECT @start=PATINDEX('%[^A-Za-z@][@]%', ' '+@contents COLLATE SQL_Latin1_General_CP850_Bin)--AAAAAAAA
SELECT @token=SUBSTRING(' '+@contents, @start+, @End-@Start-),
@endofname=PATINDEX('%[0-9]%', @token COLLATE SQL_Latin1_General_CP850_Bin),
@param=RIGHT(@token, LEN(@token)-@endofname+)
SELECT
@token=LEFT(@token, @endofname-),
@Contents=RIGHT(' '+@contents, LEN(' '+@contents+'|')-@end-)
SELECT @name=stringvalue FROM @strings
WHERE string_id=@param --fetch the name
END
ELSE
SELECT @Name=NULL,@SequenceNo=@SequenceNo+
SELECT
@end=CHARINDEX(',', @contents)-- a string-token, object-token, list-token, number,boolean, or null
IF @end=
SELECT @end=PATINDEX('%[A-Za-z0-9@+.e][^A-Za-z0-9@+.e]%', @Contents+' ' COLLATE SQL_Latin1_General_CP850_Bin)
+
SELECT
@start=PATINDEX('%[^A-Za-z0-9@+.e][A-Za-z0-9@+.e]%', ' '+@contents COLLATE SQL_Latin1_General_CP850_Bin)
--select @start,@end, LEN(@contents+'|'), @contents
SELECT
@Value=RTRIM(SUBSTRING(@contents, @start, @End-@Start)),
@Contents=RIGHT(@contents+' ', LEN(@contents+'|')-@end)
IF SUBSTRING(@value, , )='@object'
INSERT INTO @hierarchy
(NAME, SequenceNo, parent_ID, StringValue, Object_ID, ValueType)
SELECT @name, @SequenceNo, @parent_ID, SUBSTRING(@value, , ),
SUBSTRING(@value, , ), 'object'
ELSE
IF SUBSTRING(@value, , )='@array'
INSERT INTO @hierarchy
(NAME, SequenceNo, parent_ID, StringValue, Object_ID, ValueType)
SELECT @name, @SequenceNo, @parent_ID, SUBSTRING(@value, , ),
SUBSTRING(@value, , ), 'array'
ELSE
IF SUBSTRING(@value, , )='@string'
INSERT INTO @hierarchy
(NAME, SequenceNo, parent_ID, StringValue, ValueType)
SELECT @name, @SequenceNo, @parent_ID, stringvalue, 'string'
FROM @strings
WHERE string_id=SUBSTRING(@value, , )
ELSE
IF @value IN ('true', 'false')
INSERT INTO @hierarchy
(NAME, SequenceNo, parent_ID, StringValue, ValueType)
SELECT @name, @SequenceNo, @parent_ID, @value, 'boolean'
ELSE
IF @value='null'
INSERT INTO @hierarchy
(NAME, SequenceNo, parent_ID, StringValue, ValueType)
SELECT @name, @SequenceNo, @parent_ID, @value, 'null'
ELSE
IF PATINDEX('%[^0-9]%', @value COLLATE SQL_Latin1_General_CP850_Bin)>
INSERT INTO @hierarchy
(NAME, SequenceNo, parent_ID, StringValue, ValueType)
SELECT @name, @SequenceNo, @parent_ID, @value, 'real'
ELSE
INSERT INTO @hierarchy
(NAME, SequenceNo, parent_ID, StringValue, ValueType)
SELECT @name, @SequenceNo, @parent_ID, @value, 'int'
IF @Contents=' ' SELECT @SequenceNo=
END
END
INSERT INTO @hierarchy (NAME, SequenceNo, parent_ID, StringValue, Object_ID, ValueType)
SELECT '-',, NULL, '', @parent_id-, @type
--
RETURN
END
parseJSON
/****** Object: UserDefinedFunction [dbo].[ToJSON] Script Date: 2016/5/6 17:25:49 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE FUNCTION [dbo].[ToJSON]
(
@Hierarchy Hierarchy READONLY
) /*
the function that takes a Hierarchy table and converts it to a JSON string Author: Phil Factor
Revision: 1.5
date: 1 May 2014
why: Added a fix to add a name for a list.
example: Declare @XMLSample XML
Select @XMLSample='
<glossary><title>example glossary</title>
<GlossDiv><title>S</title>
<GlossList>
<GlossEntry ID="SGML" SortAs="SGML">
<GlossTerm>Standard Generalized Markup Language</GlossTerm>
<Acronym>SGML</Acronym>
<Abbrev>ISO 8879:1986</Abbrev>
<GlossDef>
<para>A meta-markup language, used to create markup languages such as DocBook.</para>
<GlossSeeAlso OtherTerm="GML" />
<GlossSeeAlso OtherTerm="XML" />
</GlossDef>
<GlossSee OtherTerm="markup" />
</GlossEntry>
</GlossList>
</GlossDiv>
</glossary>' DECLARE @MyHierarchy Hierarchy -- to pass the hierarchy table around
insert into @MyHierarchy select * from dbo.ParseXML(@XMLSample)
SELECT dbo.ToJSON(@MyHierarchy) */
RETURNS NVARCHAR(MAX)--JSON documents are always unicode.
AS
BEGIN
DECLARE
@JSON NVARCHAR(MAX),
@NewJSON NVARCHAR(MAX),
@Where INT,
@ANumber INT,
@notNumber INT,
@indent INT,
@ii INT,
@CrLf CHAR()--just a simple utility to save typing! --firstly get the root token into place
SELECT @CrLf=CHAR()+CHAR(),--just CHAR() in UNIX
@JSON = CASE ValueType WHEN 'array' THEN
+COALESCE('{'+@CrLf+' "'+NAME+'" : ','')+'['
ELSE '{' END
+@CrLf
+ CASE WHEN ValueType='array' AND NAME IS NOT NULL THEN ' ' ELSE '' END
+ '@Object'+CONVERT(VARCHAR(),OBJECT_ID)
+@CrLf+CASE ValueType WHEN 'array' THEN
CASE WHEN NAME IS NULL THEN ']' ELSE ' ]'+@CrLf+'}'+@CrLf END
ELSE '}' END
FROM @Hierarchy
WHERE parent_id IS NULL AND valueType IN ('object','document','array') --get the root element
/* now we simply iterat from the root token growing each branch and leaf in each iteration. This won't be enormously quick, but it is simple to do. All values, or name/value pairs withing a structure can be created in one SQL Statement*/
SELECT @ii=
WHILE @ii>
BEGIN
SELECT @where= PATINDEX('%[^[a-zA-Z0-9]@Object%',@json)--find NEXT token
IF @where= BREAK
/* this is slightly painful. we get the indent of the object we've found by looking backwards up the string */
SET @indent=CHARINDEX(CHAR()+CHAR(),REVERSE(LEFT(@json,@where))+CHAR()+CHAR())-
SET @NotNumber= PATINDEX('%[^0-9]%', RIGHT(@json,LEN(@JSON+'|')-@Where-)+' ')--find NEXT token
SET @NewJSON=NULL --this contains the structure in its JSON form
SELECT
@NewJSON=COALESCE(@NewJSON+','+@CrLf+SPACE(@indent),'')
+CASE WHEN parent.ValueType='array' THEN '' ELSE COALESCE('"'+TheRow.NAME+'" : ','') END
+CASE TheRow.valuetype
WHEN 'array' THEN ' ['+@CrLf+SPACE(@indent+)
+'@Object'+CONVERT(VARCHAR(),TheRow.[OBJECT_ID])+@CrLf+SPACE(@indent+)+']'
WHEN 'object' THEN ' {'+@CrLf+SPACE(@indent+)
+'@Object'+CONVERT(VARCHAR(),TheRow.[OBJECT_ID])+@CrLf+SPACE(@indent+)+'}'
WHEN 'string' THEN '"'+dbo.JSONEscaped(TheRow.StringValue)+'"'
ELSE TheRow.StringValue
END
FROM @Hierarchy TheRow
INNER JOIN @hierarchy Parent
ON parent.element_ID=TheRow.parent_ID
WHERE TheRow.parent_id= SUBSTRING(@JSON,@where+, @Notnumber-)
/* basically, we just lookup the structure based on the ID that is appended to the @Object token. Simple eh? */
--now we replace the token with the structure, maybe with more tokens in it.
SELECT @JSON=STUFF (@JSON, @where+, +@NotNumber-, @NewJSON),@ii=@ii-
END
RETURN @JSON
END GO
toJson
在Sql查询中使用parseJSON和toJson方法
Sql Server xml 类型字段的增删改查 文章中介绍了几种用法,我这里非常简单。有一张表 Candidate_Ext,CVParseJson 字段存储了json格式数据,其中有个节点是SkillName存储了技能列表,这里使用sql语句把技能名称查出来逗号分隔。
"SkillList": [
{
"SkillId": ,
"CandidateId": ,
"TenantId": ,
"SkillName": "Oracle",
"SkillLevel": ,
"SkillLevelName": "熟练",
"SkillLevelName1": null
},
{
"SkillId": ,
"CandidateId": ,
"TenantId": ,
"SkillName": "TCP/IP",
"SkillLevel": ,
"SkillLevelName": "精通",
"SkillLevelName1": null
}
],
sql语句:
SELECT TOP l.CandidateId ,l.CVParseJson,
( SELECT StringValue+','
FROM parseJSON(l.CVParseJson) json
WHERE json.NAME='SkillName'
FOR
XML PATH('')
) 专业技能
FROM dbo.Candidate_Ext l
WHERE CVParseJson LIKE '{"candi%'
结果:
CandidateId 专业技能
Oracle,TCP/IP,
如果多个节点都有SkillName 这个属性处理起来就比较麻烦了,性能也不见得好,所以小数据出来使用这个方法还是比较方便的。