SQL Server
SQL Server
Pretty Print JSON (Formatter, Beautifier)
See more JSON Examples
Demonstrates how to emit JSON in a pretty, human-readable format with indenting of nested arrays and objects.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @jsonStr nvarchar(4000)
SELECT @jsonStr = '{"name": "donut","image":{"fname": "donut.jpg","w": 200,"h": 200},"thumbnail":{"fname": "donutThumb.jpg","w": 32,"h": 32}}'
EXEC sp_OAMethod @json, 'Load', @success OUT, @jsonStr
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @json, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @json
RETURN
END
-- To pretty-print, set the EmitCompact property equal to 0
EXEC sp_OASetProperty @json, 'EmitCompact', 0
-- If bare-LF line endings are desired, turn off EmitCrLf
-- Otherwise CRLF line endings are emitted.
EXEC sp_OASetProperty @json, 'EmitCrLf', 0
-- Emit the formatted JSON:
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @json
END
GO