SQL Server
SQL Server
Copy JSON Object from one JSON Array to Another
See more JSON Examples
Demonstrates how to copy an object in a JSON array to another JSON array.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 @arr1 int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @arr1 OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @arr2 int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @arr2 OUT
DECLARE @s nvarchar(4000)
SELECT @s = '[{"a":1}, {"b":2}, {"c":3}]'
DECLARE @sEmpty nvarchar(4000)
SELECT @sEmpty = '[]'
DECLARE @success int
EXEC sp_OAMethod @arr1, 'Load', @success OUT, @s
EXEC sp_OAMethod @arr2, 'Load', @success OUT, @sEmpty
DECLARE @jObj int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jObj OUT
EXEC sp_OAMethod @arr1, 'ObjectAt2', @success OUT, 1, @jObj
EXEC sp_OAMethod @arr2, 'AddObjectCopyAt', @success OUT, -1, @jObj
EXEC sp_OAMethod @arr2, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- output is: [{"b":2}]
EXEC @hr = sp_OADestroy @arr1
EXEC @hr = sp_OADestroy @arr2
EXEC @hr = sp_OADestroy @jObj
END
GO