APPX Software Library

Chapter 7: Read APPX Data with 32-bit PowerShell

Export COUNTRY to CSV and JSON

Keep the unformatted query objects in $rows. Choose a new local filename; -NoClobber prevents replacing an existing CSV:

$rows | Select-Object name, region_label, no_words, post_code_loc |
    Export-Csv -LiteralPath .\COUNTRY.csv -NoTypeInformation -Encoding UTF8 -NoClobber
$csv = @(Import-Csv -LiteralPath .\COUNTRY.csv)
$csv.Count
$csv | Format-Table -AutoSize

Expect 7 data rows. Windows PowerShell 5.1's UTF8 encoding writes a byte-order mark; CSV quotes and escaping are handled by Export-Csv. Its default separator is a comma. Use -UseCulture on both export and import when the receiving application's regional separator is required. CSV re-import produces strings, so cast numeric fields before calculations. CSV is a snapshot; opening it in Calc or Excel does not create an ODBC refresh connection. See Microsoft's Export-Csv reference.

For JSON, select only the business properties needed by the recipient. Preserve an array even when a filter returns one record:

$jsonRows = @($rows | Select-Object name, region_label, no_words, post_code_loc)
$json = ConvertTo-Json -InputObject $jsonRows -Depth 4
if (Test-Path -LiteralPath .\COUNTRY.json) { throw 'Choose a new JSON filename' }
$json | Set-Content -LiteralPath .\COUNTRY.json -Encoding UTF8
$loaded = Get-Content -LiteralPath .\COUNTRY.json -Raw | ConvertFrom-Json
$loaded.Count

Expect 7 objects, with no_words still numeric. In Windows PowerShell 5.1, assign ConvertFrom-Json directly as shown; wrapping that command in @(...) can produce an outer array containing the result array and a misleading count of 1. This example deliberately selects four fields. If audit dates are included, choose an explicit interchange format: Windows PowerShell 5.1's default DateTime JSON representation is not an ISO 8601 string. Convert a non-null date using $value.ToString('o') when the recipient requires ISO 8601; do not invent a timezone for an APPX date that has none. Exported files contain data and must be stored with the same care as the source records.