Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Insert JSON Object into another JSON ObjectDemonstrates how to insert one JSON object into another. Effectively, the JSON object must be copied into the other..
-- 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) -- Imagine we have two separate JSON objects. DECLARE @jsonA int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonA OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @jsonA, 'UpdateString', @success OUT, 'animal', 'zebra' EXEC sp_OAMethod @jsonA, 'UpdateString', @success OUT, 'colors[0]', 'white' EXEC sp_OAMethod @jsonA, 'UpdateString', @success OUT, 'colors[1]', 'black' EXEC sp_OASetProperty @jsonA, 'EmitCompact', 0 EXEC sp_OAMethod @jsonA, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- jsonA contains: -- { -- "animal": "zebra", -- "colors": [ -- "white", -- "black" -- ] -- } DECLARE @jsonB int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonB OUT EXEC sp_OAMethod @jsonB, 'UpdateString', @success OUT, 'type', 'mammal' EXEC sp_OAMethod @jsonB, 'UpdateBool', @success OUT, 'carnivore', 0 EXEC sp_OASetProperty @jsonB, 'EmitCompact', 0 EXEC sp_OAMethod @jsonB, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- jsonB contains: -- { -- "type": "mammal", -- "carnivore": false -- } -- Let's say we want to insert jsonB into jsonA to get this: -- { -- "animal": "zebra", -- "info" " { -- "type": "mammal", -- "carnivore": false -- }, -- "colors": [ -- "white", -- "black" -- ] -- } -- First add an empty object at the desired location: EXEC sp_OAMethod @jsonA, 'AddObjectAt', @success OUT, 1, 'info' -- Get the JSON object at that location, and load the JSON.. DECLARE @jsonInfo int EXEC sp_OAMethod @jsonA, 'ObjectOf', @jsonInfo OUT, 'info' EXEC sp_OAMethod @jsonB, 'Emit', @sTmp0 OUT EXEC sp_OAMethod @jsonInfo, 'Load', @success OUT, @sTmp0 EXEC @hr = sp_OADestroy @jsonInfo EXEC sp_OAMethod @jsonA, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- The end result is this: -- { -- "animal": "zebra", -- "info": { -- "type": "mammal", -- "carnivore": false -- }, -- "colors": [ -- "white", -- "black" -- ] -- } EXEC @hr = sp_OADestroy @jsonA EXEC @hr = sp_OADestroy @jsonB END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.