Sample code for 30+ languages & platforms
SQL Server

Extract PDF from JSON

See more JSON Examples

Demonstrates how to extract a PDF file contained within JSON. The file is represented as a base64 string within the JSON. Note: This example can extract any type of file, not just a PDF file.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr 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

    --  Load the JSON.
    EXEC sp_OAMethod @json, 'LoadFile', @success OUT, 'qa_data/json/JSR5U.json'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @json, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    --  The JSON we loaded contains this:

    --  	{
    --  	...
    --  	...
    --  	  "data": {
    --  	    "content": "JVBERi0xLjQ..."
    --  	  }
    --  	...
    --  	...
    --  	}

    DECLARE @sb int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT

    EXEC sp_OAMethod @json, 'StringOfSb', @success OUT, 'data.content', @sb

    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    EXEC sp_OAMethod @bd, 'AppendEncodedSb', @success OUT, @sb, 'base64'

    EXEC sp_OAMethod @bd, 'WriteFile', @success OUT, 'qa_output/a0015.pdf'

    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sb
    EXEC @hr = sp_OADestroy @bd


END
GO