Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

JSON: Miscellaneous Operations

See more JSON Examples

Demonstrates a variety of JSON API methods. This example uses the following JSON document:
{
   "alphabet": "abcdefghijklmnopqrstuvwxyz",
   "sampleData" : {
           "pi": 3.14,
	   "apple": "juicy",
	   "hungry": true,
	   "withoutValue": null,
           "answer": 42
          
	}
}

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @json, 'EmitCompact', 0

    --  Assume the file contains the data as shown above..
    EXEC sp_OAMethod @json, 'LoadFile', @success OUT, 'qa_data/json/sample2.json'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @json, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    --  Get the "sampleData" object:
    DECLARE @sampleData int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @sampleData OUT

    EXEC sp_OAMethod @json, 'ObjectOf2', @success OUT, 'sampleData', @sampleData

    --  Demonstrate BoolAt and BoolOf

    EXEC sp_OAMethod @sampleData, 'BoolOf', @iTmp0 OUT, 'hungry'
    PRINT 'hungry: ' + @iTmp0

    EXEC sp_OAMethod @sampleData, 'BoolAt', @iTmp0 OUT, 2
    PRINT 'hungry: ' + @iTmp0

    --  StringOf returns the value as a string regardless of it's actual type:

    EXEC sp_OAMethod @sampleData, 'StringOf', @sTmp0 OUT, 'pi'
    PRINT 'pi: ' + @sTmp0

    EXEC sp_OAMethod @sampleData, 'StringOf', @sTmp0 OUT, 'answer'
    PRINT 'answer: ' + @sTmp0

    EXEC sp_OAMethod @sampleData, 'StringOf', @sTmp0 OUT, 'withoutValue'
    PRINT 'withoutValue: ' + @sTmp0

    EXEC sp_OAMethod @sampleData, 'StringOf', @sTmp0 OUT, 'hungry'
    PRINT 'hungry: ' + @sTmp0

    --  Demonstrate IsNullOf / IsNullAt

    EXEC sp_OAMethod @sampleData, 'IsNullOf', @iTmp0 OUT, 'withoutValue'
    PRINT 'withoutValue is null? ' + @iTmp0

    EXEC sp_OAMethod @sampleData, 'IsNullAt', @iTmp0 OUT, 3
    PRINT 'withoutValue is null? ' + @iTmp0

    EXEC sp_OAMethod @sampleData, 'IsNullOf', @iTmp0 OUT, 'apple'
    PRINT 'apple is null? ' + @iTmp0

    EXEC sp_OAMethod @sampleData, 'IsNullAt', @iTmp0 OUT, 1
    PRINT 'apple is null? ' + @iTmp0

    --  IntOf

    EXEC sp_OAMethod @sampleData, 'IntOf', @iTmp0 OUT, 'answer'
    PRINT 'answer: ' + @iTmp0

    --  SetNullAt, SetNullOf
    --  Set "pi" to null
    EXEC sp_OAMethod @sampleData, 'SetNullAt', @success OUT, 0
    --  Set "answer" to null
    EXEC sp_OAMethod @sampleData, 'SetNullOf', @success OUT, 'answer'

    --  Show the changes:
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    --  Restore pi and apple:
    EXEC sp_OAMethod @sampleData, 'SetNumberAt', @success OUT, 0, '3.14'
    EXEC sp_OAMethod @sampleData, 'SetNumberOf', @success OUT, 'answer', '42'

    --  Show the changes:
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    --  Add a null value named "afterApple" just after "apple"
    EXEC sp_OAMethod @sampleData, 'AddNullAt', @success OUT, 2, 'afterApple'

    --  Add a boolean value just after "pi"
    EXEC sp_OAMethod @sampleData, 'AddBoolAt', @success OUT, 1, 'afterPi', 0

    --  Examine the changes..
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sampleData


END
GO