Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Get the JSON we'll be parsing..
local jsonStr = http:quick_get_str("https://www.chilkatsoft.com/exampleData/qb_customer_balance_detail_report_2.json")
if not http.last_method_success then
print(http.last_error_text)
return
end
local json = chilkat.JsonObject.new()
json:load(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.
local csv = chilkat.Csv.new()
csv.has_column_names = true
-- Set the column names of the CSV.
local numColumns = json:size_of_array("Columns.Column")
if numColumns < 0 then
print("Unable to get column names")
return
end
local i = 0
while i < numColumns do
json.i = i
csv:set_column_name(i, json:string_of("Columns.Column[i].ColTitle"))
i = i + 1
end
-- Let's get the rows.
-- We'll ignore the Header and Summary, and just get the data.
local row = 0
local numRows = json:size_of_array("Rows.Row[0].Rows.Row")
if numRows < 0 then
print("Unable to get data rows")
return
end
while row < numRows do
json.i = row
numColumns = json:size_of_array("Rows.Row[0].Rows.Row[i].ColData")
local col = 0
while col < numColumns do
json.j = col
csv:set_cell(row, col, json:string_of("Rows.Row[0].Rows.Row[i].ColData[j].value"))
col = col + 1
end
row = row + 1
end
-- Show the CSV
print(csv:save_to_string())
-- Save to a CSV file
local success = csv:save_file("qa_output/customerDetailReport.csv")