Skip to content

Commit 01e1303

Browse files
committed
First Version of SQL Server Indent JSON CLR function
First Version of SQL Server Indent JSON CLR function
0 parents  commit 01e1303

18 files changed

Lines changed: 553 additions & 0 deletions
36 KB
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "JSONBeautifier", "JSONBeautifier\JSONBeautifier.sqlproj", "{76AC8278-C575-46F4-9526-B6A82A54C331}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{76AC8278-C575-46F4-9526-B6A82A54C331}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{76AC8278-C575-46F4-9526-B6A82A54C331}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{76AC8278-C575-46F4-9526-B6A82A54C331}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{76AC8278-C575-46F4-9526-B6A82A54C331}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{76AC8278-C575-46F4-9526-B6A82A54C331}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{76AC8278-C575-46F4-9526-B6A82A54C331}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
EndGlobal
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
/* *********************************************************************************************************************************************************************/
3+
-- Drop the Assembly if already Exists
4+
/* *********************************************************************************************************************************************************************/
5+
6+
IF EXISTS
7+
(
8+
SELECT * FROM SYS.assemblies
9+
WHERE Name = 'JSONBeautifier'
10+
)
11+
BEGIN
12+
13+
DROP ASSEMBLY [JSONBeautifier]
14+
15+
END
16+
GO
17+
18+
/* *********************************************************************************************************************************************************************/
19+
-- Create the Assembly
20+
/* *********************************************************************************************************************************************************************/
21+
22+
CREATE ASSEMBLY [JSONBeautifier] AUTHORIZATION [dbo]
23+
FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C01030082F390590000000000000000E00002210B010B00000C000000060000000000003E2A0000002000000040000000000010002000000002000004000000000000000600000000000000008000000002000000000000030060850000100000100000000010000010000000000000100000000000000000000000E42900005700000000400000C002000000000000000000000000000000000000006000000C000000AC2800001C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E74657874000000440A000000200000000C000000020000000000000000000000000000200000602E72737263000000C00200000040000000040000000E0000000000000000000000000000400000402E72656C6F6300000C0000000060000000020000001200000000000000000000000000004000004200000000000000000000000000000000202A000000000000480000000200050000230000AC0500000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000133001001300000001000011000228020000060A06730500000A0B2B00072A001B300300510200000200001100160A160B730600000A0C160D38200200000002096F0700000A13041104130911091F2C301711091F223B2401000011091F2C3B6501000038E501000011091F3A3BBE01000011091F5B5945030000001B000000C60100008900000011091F7B59450300000005000000B00100007300000038AB0100000811046F0800000A2607130A110A2D5900086F0900000A260016061758250A280A00000A6F0B00000A130B2B17110B6F0C00000A1305000872010000706F0D00000A2600110B6F0E00000A130A110A2DDCDE14110B14FE01130A110A2D08110B6F0F00000A00DC0000384801000007130A110A2D5900086F0900000A260016061759250A280A00000A6F0B00000A130B2B17110B6F0C00000A1305000872010000706F0D00000A2600110B6F0E00000A130A110A2DDCDE14110B14FE01130A110A2D08110B6F0F00000A00DC00000811046F0800000A2638DA0000000811046F0800000A261613060913072B07110616FE011306110716311302110717592513076F0700000A1F5CFE012B011600130A110A2DD91106130A110A2D050716FE010B38900000000811046F0800000A2607130A110A2D5500086F0900000A26001606280A00000A6F0B00000A130B2B17110B6F0C00000A1305000872010000706F0D00000A2600110B6F0E00000A130A110A2DDCDE14110B14FE01130A110A2D08110B6F0F00000A00DC00002B290811046F0800000A2607130A110A2D0C08720B0000706F0D00000A262B0B0811046F0800000A262B00000917580D09026F1000000AFE04130A110A3ACEFDFFFF086F1100000A13082B0011082A000000012800000200A20028CA00140000000002000701282F0114000000000200C40128EC0114000000001E02281200000A2A42534A4201000100000000000C00000076342E302E33303331390000000005006C000000E8010000237E0000540200006C02000023537472696E677300000000C00400001000000023555300D0040000100000002347554944000000E0040000CC00000023426C6F620000000000000002000001571D02080900000000FA253300160000010000000E000000020000000100000003000000020000001200000001000000040000000200000002000000010000000300000000000A0001000000000006003C0035000A0072005D000600B400A1000F00C80000000600F700D70006001701D7000A005001350106007101650106007F0135000E00BA01AE010600E001C5010600F401C50106002F021C020600440235000000000001000000000001000100010010001D000000050001000100518043000A0050200000000096007C0016000100702000000000960087001C000200F822000000008618920021000300000001009800000001009D00190092002500290092002B00310092002100390092002100110092003500410092002100490086014000410090014500410097014B005100EE0150000C0002026000140010026F0041009001740069003B027A00710050022100490058027E000900630282000900920021000E0004000D002000230030002E000B009A002E001300A3002E001B00AC003A0086005A0069000480000000000000000000000000000000001D00000004000000000000000000000001002C000000000004000000000000000000000001005100000000000400000000000000000000000100A201000000000000003C4D6F64756C653E004A534F4E426561757469666965722E646C6C004A534F4E42656175746966696572006D73636F726C69620053797374656D004F626A65637400494E44454E545F535452494E470053797374656D2E446174610053797374656D2E446174612E53716C54797065730053716C537472696E6700496E64656E744A534F4E00466F726D61744A736F6E002E63746F72004A534F4E007374720053797374656D2E446961676E6F73746963730044656275676761626C6541747472696275746500446562756767696E674D6F6465730053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300436F6D70696C6174696F6E52656C61786174696F6E734174747269627574650052756E74696D65436F6D7061746962696C697479417474726962757465004D6963726F736F66742E53716C5365727665722E5365727665720053716C46756E6374696F6E4174747269627574650053797374656D2E5465787400537472696E674275696C64657200537472696E67006765745F436861727300417070656E6400417070656E644C696E650053797374656D2E436F72650053797374656D2E4C696E7100456E756D657261626C650053797374656D2E436F6C6C656374696F6E732E47656E657269630049456E756D657261626C6560310052616E67650049456E756D657261746F72603100476574456E756D657261746F72006765745F43757272656E740053797374656D2E436F6C6C656374696F6E730049456E756D657261746F72004D6F76654E6578740049446973706F7361626C6500446973706F7365006765745F4C656E67746800546F537472696E670000092000200020002000000320000000C286D4310376AF41AABC8E0927E32A3A0008B77A5C561934E08902060E08200020002000200005000111090E0400010E0E0320000105200101111104200101080401000000042001010E0507020E11090420010308052001122103042000122109000215122D010808080515122D0108082000151231011300051512310108042000130005200112210E03200002032000080320000E13070C0802122108030802080E030215123101080801000701000000000801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F777301000000000082F3905900000000020000001C010000C8280000C80A000052534453F5C2AB49B49E544E89ADCC4C92A4F20F04000000653A5C446576656C6F706D656E745C43235C4A534F4E426561757469666965725C4A534F4E426561757469666965725C6F626A5C44656275675C4A534F4E426561757469666965722E706462000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000C2A000000000000000000002E2A0000002000000000000000000000000000000000000000000000202A00000000000000000000000000000000000000005F436F72446C6C4D61696E006D73636F7265652E646C6C0000000000FF250020001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100100000001800008000000000000000000000000000000100010000003000008000000000000000000000000000000100000000004800000058400000640200000000000000000000640234000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000000000000000000000000000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004C4010000010053007400720069006E006700460069006C00650049006E0066006F000000A001000001003000300030003000300034006200300000002C0002000100460069006C0065004400650073006300720069007000740069006F006E000000000020000000300008000100460069006C006500560065007200730069006F006E000000000030002E0030002E0030002E003000000048001300010049006E007400650072006E0061006C004E0061006D00650000004A0053004F004E0042006500610075007400690066006900650072002E0064006C006C00000000002800020001004C006500670061006C0043006F0070007900720069006700680074000000200000005000130001004F0072006900670069006E0061006C00460069006C0065006E0061006D00650000004A0053004F004E0042006500610075007400690066006900650072002E0064006C006C0000000000340008000100500072006F006400750063007400560065007200730069006F006E00000030002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000030002E0030002E0030002E0030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000C000000403A00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
24+
WITH PERMISSION_SET = SAFE
25+
GO
26+
27+
/* *********************************************************************************************************************************************************************/
28+
-- Drop the Function if Already Exists
29+
/* *********************************************************************************************************************************************************************/
30+
31+
IF EXISTS (SELECT * FROM SYS.ALL_OBJECTS WHERE NAME = 'IndentJSON' AND TYPE ='FS')
32+
BEGIN
33+
DROP FUNCTION [dbo].[IndentJSON]
34+
END
35+
GO
36+
37+
/* *********************************************************************************************************************************************************************/
38+
-- Create the Function if Already Exists
39+
/* *********************************************************************************************************************************************************************/
40+
CREATE FUNCTION [dbo].[IndentJSON] (@JSON [nvarchar](MAX))
41+
RETURNS [nvarchar](MAX)
42+
AS EXTERNAL NAME [JSONBeautifier].[JSONBeautifier].[IndentJSON];
43+
GO
44+
45+
/* *********************************************************************************************************************************************************************/
46+
-- Run SP_CONFIGURE to enable CLR
47+
/* *********************************************************************************************************************************************************************/
48+
49+
EXEC SP_CONFIGURE 'clr enabled',1
50+
GO
51+
52+
/* *********************************************************************************************************************************************************************/
53+
-- Run RECONFIGURE to if above SP_CONFIGURE is run
54+
/* *********************************************************************************************************************************************************************/
55+
RECONFIGURE
56+
GO
57+
58+
/* *********************************************************************************************************************************************************************/
59+
-- Example 1 - With directly passing the JSON string
60+
/* *********************************************************************************************************************************************************************/
61+
SELECT [dbo].[IndentJSON]('{"id": 1,"name": "A green door","price": 12.50,"tags": ["home", "green"]}')
62+
GO
63+
64+
/* *********************************************************************************************************************************************************************/
65+
-- Example 2 - Query from a Table
66+
/* *********************************************************************************************************************************************************************/
67+
SELECT [dbo].[IndentJSON](
68+
LTRIM
69+
(
70+
(
71+
SELECT TOP 2 A.ADDRESSID
72+
,A.CITY AS "REGION.CITY"
73+
,A.COUNTRYREGION AS "REGION.COUNTRYREGION"
74+
FROM ADVENTUREWORKSLT2012.SALESLT.ADDRESS A
75+
FOR JSON PATH , ROOT('Region')
76+
)
77+
)
78+
)
79+
80+
GO
81+
82+
/* *********************************************************************************************************************************************************************/
83+
-- Example 3 - Query from a Table to get the JSON into a variable and format it
84+
/* *********************************************************************************************************************************************************************/
85+
86+
DECLARE @X NVARCHAR(max)
87+
88+
SELECT @X =
89+
(select top 2 a.AddressID
90+
,a.City as "Region.City"
91+
,a.CountryRegion as "Region.CountryRegion"
92+
from AdventureWorksLT2012.SalesLT.Address a
93+
for JSON path
94+
)
95+
96+
SELECT [dbo].[IndentJSON](@X)
97+
98+
GO
99+
100+
101+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.SqlClient;
4+
using System.Data.SqlTypes;
5+
using Microsoft.SqlServer.Server;
6+
7+
using System.Text;
8+
using System.Linq;
9+
10+
11+
public partial class JSONBeautifier
12+
{
13+
[Microsoft.SqlServer.Server.SqlFunction]
14+
public static SqlString IndentJSON(string JSON)
15+
{
16+
// Put your code here
17+
string FormatedJson = FormatJson(JSON);
18+
return new SqlString (FormatedJson);
19+
}
20+
21+
private const string INDENT_STRING = " ";
22+
public static string FormatJson(string str)
23+
{
24+
var indent = 0;
25+
var quoted = false;
26+
var sb = new StringBuilder();
27+
for (var i = 0; i < str.Length; i++)
28+
{
29+
var ch = str[i];
30+
switch (ch)
31+
{
32+
case '{':
33+
case '[':
34+
sb.Append(ch);
35+
if (!quoted)
36+
{
37+
sb.AppendLine();
38+
39+
foreach (int item in Enumerable.Range(0, ++indent))
40+
{
41+
sb.Append(INDENT_STRING);
42+
}
43+
44+
}
45+
break;
46+
case '}':
47+
case ']':
48+
if (!quoted)
49+
{
50+
sb.AppendLine();
51+
52+
foreach (int item in Enumerable.Range(0, --indent))
53+
{
54+
sb.Append(INDENT_STRING);
55+
}
56+
}
57+
sb.Append(ch);
58+
break;
59+
case '"':
60+
sb.Append(ch);
61+
bool escaped = false;
62+
var index = i;
63+
while (index > 0 && str[--index] == '\\')
64+
escaped = !escaped;
65+
if (!escaped)
66+
quoted = !quoted;
67+
break;
68+
case ',':
69+
sb.Append(ch);
70+
if (!quoted)
71+
{
72+
sb.AppendLine();
73+
74+
foreach (int item in Enumerable.Range(0, indent))
75+
{
76+
sb.Append(INDENT_STRING);
77+
}
78+
}
79+
break;
80+
case ':':
81+
sb.Append(ch);
82+
if (!quoted)
83+
sb.Append(" ");
84+
break;
85+
default:
86+
sb.Append(ch);
87+
break;
88+
}
89+
}
90+
return sb.ToString();
91+
}
92+
93+
94+
}
6.26 MB
Binary file not shown.
16 KB
Binary file not shown.

0 commit comments

Comments
 (0)