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) Get the Root of a JSON DocumentDemonstrates how to get back to the JSON root object from anywhere in the JSON document. This example uses the following JSON document: { "flower": "tulip", "abc": { "x": [ { "a" : 1 }, { "b1" : 100, "b2" : 200 }, { "c" : 3 } ], "y": 200, "z": 200 } }
-- 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 @json int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 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 = '{"flower": "tulip","abc":{"x": [{ "a" : 1 },{ "b1" : 100, "b2" : 200 },{ "c" : 3 }],"y": 200,"z": 200}}' DECLARE @success int 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 -- Get the "abc" object. DECLARE @abcObj int EXEC sp_OAMethod @json, 'ObjectOf', @abcObj OUT, 'abc' EXEC sp_OAGetProperty @json, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN PRINT 'abc object not found.' EXEC @hr = sp_OADestroy @json RETURN END -- Side note: The JSON of a sub-part of the document can be emitted from any JSON object: EXEC sp_OASetProperty @abcObj, 'EmitCompact', 0 EXEC sp_OAMethod @abcObj, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- Navigate to the "x" array DECLARE @xArray int EXEC sp_OAMethod @abcObj, 'ArrayOf', @xArray OUT, 'x' -- We'll skip the null check and assume it's non-null... -- Navigate to the 2nd object contained within the array. This contains members b1 and b2 DECLARE @bObj int EXEC sp_OAMethod @xArray, 'ObjectAt', @bObj OUT, 1 -- We'll skip the null check and assume it's non-null... -- Show that we're at "b1/b2". -- The value of "b1" should be "200" EXEC sp_OAMethod @bObj, 'IntOf', @iTmp0 OUT, 'b2' PRINT 'b2 = ' + @iTmp0 -- Now go back to the JSON doc root: DECLARE @docRoot int EXEC sp_OAMethod @bObj, 'GetDocRoot', @docRoot OUT -- We'll skip the null check and assume it's non-null... -- Pretty-print the JSON doc from the root to show that this is indeed the root. EXEC sp_OASetProperty @docRoot, 'EmitCompact', 0 EXEC sp_OAMethod @docRoot, 'Emit', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @docRoot EXEC @hr = sp_OADestroy @bObj EXEC @hr = sp_OADestroy @xArray EXEC @hr = sp_OADestroy @abcObj EXEC @hr = sp_OADestroy @json END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.