SQL Server
SQL Server
Load a JSON Array
See more JSON Examples
The Chilkat JSON API requires the top-level JSON to be an object. Therefore, to load an array requires that it first be wrapped as an object.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
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Imagine we want to load this JSON array for parsing:
DECLARE @jsonArrayStr nvarchar(4000)
SELECT @jsonArrayStr = '[{"id":200},{"id":196}]'
-- First wrap it in a JSON object by prepending "{ "array":" and appending "}"
DECLARE @sbJson int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJson OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @sbJson, 'Append', @success OUT, '{"array":'
EXEC sp_OAMethod @sbJson, 'Append', @success OUT, @jsonArrayStr
EXEC sp_OAMethod @sbJson, 'Append', @success OUT, '}'
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @sbJson, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0
-- Now we can get the JSON array
DECLARE @jArray int
EXEC sp_OAMethod @json, 'ArrayAt', @jArray OUT, 0
-- Do what you want with the JSON array...
-- For example:
DECLARE @jObjId int
EXEC sp_OAMethod @jArray, 'ObjectAt', @jObjId OUT, 0
EXEC sp_OAMethod @jObjId, 'IntOf', @iTmp0 OUT, 'id'
PRINT @iTmp0
EXEC @hr = sp_OADestroy @jObjId
EXEC @hr = sp_OADestroy @jArray
EXEC @hr = sp_OADestroy @sbJson
EXEC @hr = sp_OADestroy @json
END
GO