Sample code for 30+ languages & platforms
SQL Server

JSON Copy Objects

See more JSON Examples

Copy objects from one JSON document to another.

Chilkat SQL Server Downloads

SQL Server
-- 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 @json1 int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json1 OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int
    EXEC sp_OAMethod @json1, 'UpdateString', @success OUT, 'ID1.cn', 'Name'
    EXEC sp_OAMethod @json1, 'UpdateString', @success OUT, 'ID1.objectGUID', 'GUID'
    EXEC sp_OAMethod @json1, 'UpdateString', @success OUT, 'ID2.cn', 'Name'
    EXEC sp_OAMethod @json1, 'UpdateString', @success OUT, 'ID2.objectGUID', 'GUID'

    EXEC sp_OASetProperty @json1, 'EmitCompact', 0
    EXEC sp_OAMethod @json1, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- json1 contains:
    -- {
    --   "ID1": {
    --     "cn": "Name",
    --     "objectGUID": "GUID"
    --   },
    --   "ID2": {
    --     "cn": "Name",
    --     "objectGUID": "GUID"
    --   }
    -- }

    DECLARE @json2 int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json2 OUT

    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'Name1.ID1.cn', 'Name'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'Name1.ID1.objectGUID', 'GUID'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'Name1.ID2.cn', 'Name'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'Name1.ID2.objectGUID', 'GUID'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'Name2.ID3.cn', 'Name'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'Name2.ID3.objectGUID', 'GUID'

    EXEC sp_OASetProperty @json2, 'EmitCompact', 0
    EXEC sp_OAMethod @json2, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- {
    --   "Name1": {
    --     "ID1": {
    --       "cn": "Name",
    --       "objectGUID": "GUID"
    --     },
    --     "ID2": {
    --       "cn": "Name",
    --       "objectGUID": "GUID"
    --     }
    --   },
    --   "Name2": {
    --     "ID3": {
    --       "cn": "Name",
    --       "objectGUID": "GUID"
    --     }
    --   }
    -- }

    -- Copy Name1, Name2 into json1

    DECLARE @i int
    SELECT @i = 0
    DECLARE @numMembers int
    EXEC sp_OAGetProperty @json2, 'Size', @numMembers OUT
    WHILE @i < @numMembers
      BEGIN
        DECLARE @jsonObj int
        EXEC sp_OAMethod @json2, 'ObjectAt', @jsonObj OUT, @i
        EXEC sp_OAMethod @json2, 'NameAt', @sTmp0 OUT, @i
        EXEC sp_OAMethod @json1, 'AppendObjectCopy', @success OUT, @sTmp0, @jsonObj
        SELECT @i = @i + 1
      END

    -- Now see what json1 contains:
    EXEC sp_OAMethod @json1, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- {
    --   "ID1": {
    --     "cn": "Name",
    --     "objectGUID": "GUID"
    --   },
    --   "ID2": {
    --     "cn": "Name",
    --     "objectGUID": "GUID"
    --   },
    --   "Name1": {
    --     "ID1": {
    --       "cn": "Name",
    --       "objectGUID": "GUID"
    --     },
    --     "ID2": {
    --       "cn": "Name",
    --       "objectGUID": "GUID"
    --     }
    --   },
    --   "Name2": {
    --     "ID3": {
    --       "cn": "Name",
    --       "objectGUID": "GUID"
    --     }
    --   }
    -- }

    EXEC @hr = sp_OADestroy @json1
    EXEC @hr = sp_OADestroy @json2


END
GO