Sample code for 30+ languages & platforms
SQL Server

QuickBooks - Parse the JSON of a Customer Balance Detail Report

See more CSV Examples

This example is to show how to parse the JSON of a particular Quickbooks report. The techniques shown here may help in parsing similar reports.

The JSON to be parsed is available at Sample Quickbooks Customer Balance Detail Report JSON

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
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

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

    -- Get the JSON we'll be parsing..
    DECLARE @jsonStr nvarchar(4000)
    EXEC sp_OAMethod @http, 'QuickGetStr', @jsonStr OUT, 'https://www.chilkatsoft.com/exampleData/qb_customer_balance_detail_report_2.json'
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        RETURN
      END

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

    EXEC sp_OAMethod @json, 'Load', @success OUT, @jsonStr

    -- As an alternative to manually writing code, use this online tool to generate parsing code from sample JSON: 
    -- Generate Parsing Code from JSON

    -- Let's parse the JSON into a CSV, and then save to a CSV file.
    DECLARE @csv int
    EXEC @hr = sp_OACreate 'Chilkat.Csv', @csv OUT

    EXEC sp_OASetProperty @csv, 'HasColumnNames', 1

    -- Set the column names of the CSV.
    DECLARE @numColumns int
    EXEC sp_OAMethod @json, 'SizeOfArray', @numColumns OUT, 'Columns.Column'
    IF @numColumns < 0
      BEGIN

        PRINT 'Unable to get column names'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @csv
        RETURN
      END
    DECLARE @i int
    SELECT @i = 0
    WHILE @i < @numColumns
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'Columns.Column[i].ColTitle'
        EXEC sp_OAMethod @csv, 'SetColumnName', @success OUT, @i, @sTmp0
        SELECT @i = @i + 1
      END

    -- Let's get the rows.
    -- We'll ignore the Header and Summary, and just get the data.
    DECLARE @row int
    SELECT @row = 0
    DECLARE @numRows int
    EXEC sp_OAMethod @json, 'SizeOfArray', @numRows OUT, 'Rows.Row[0].Rows.Row'
    IF @numRows < 0
      BEGIN

        PRINT 'Unable to get data rows'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @csv
        RETURN
      END

    WHILE @row < @numRows
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @row
        EXEC sp_OAMethod @json, 'SizeOfArray', @numColumns OUT, 'Rows.Row[0].Rows.Row[i].ColData'
        DECLARE @col int
        SELECT @col = 0
        WHILE @col < @numColumns
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @col
            EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'Rows.Row[0].Rows.Row[i].ColData[j].value'
            EXEC sp_OAMethod @csv, 'SetCell', @success OUT, @row, @col, @sTmp0
            SELECT @col = @col + 1
          END
        SELECT @row = @row + 1
      END

    -- Show the CSV 
    EXEC sp_OAMethod @csv, 'SaveToString', @sTmp0 OUT
    PRINT @sTmp0

    -- Save to a CSV file
    EXEC sp_OAMethod @csv, 'SaveFile', @success OUT, 'qa_output/customerDetailReport.csv'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @csv


END
GO