Reconcile XML invoices and JSON payments with XQuery

Sum split payments and find discrepancies between an ERP invoice export and a payment provider’s JSON export in one XQuery 3.1 query.

DCoding.Xml 1.2.0Example: Community

Packages: DCoding.Xml.XQuery

On this page

Match by invoice reference and currency

The XML file contains invoice references, currencies and totals. In the JSON file, each payment carries an invoice reference, currency and amount. The example treats both exports as snapshots of booked transactions. It matches a payment to an invoice only when reference and currency agree.

  • Use exports from the same reporting period with unique invoice and payment IDs. A payment in another currency is listed for review.
  • The JSON export represents amounts as decimal strings. XQuery converts them to xs:decimal before adding or comparing them.

Build the reconciliation report

The .NET host passes the XML as the context document and the JSON text as an external variable. parse-json turns the payment records into XQuery maps. The query calculates the balance per invoice and emits a separate list of unmatched payments. The host serializes the resulting map as JSON.

XQuery 3.1
xquery version "3.1";
declare namespace ar = "urn:example:invoices";
declare variable $payment-json as xs:string external;

let $invoices := /ar:invoices/ar:invoice
let $payments := parse-json($payment-json)?payments?*
return map {
  "invoices": array {
    for $invoice in $invoices
    let $reference := string($invoice/@id)
    let $currency := string($invoice/@currency)
    let $expected := xs:decimal($invoice/@total)
    let $matched := $payments[?invoice = $reference and ?currency = $currency]
    let $paid := sum(for $payment in $matched return xs:decimal($payment?amount), xs:decimal(0))
    let $balance := $expected - $paid
    order by $reference
    return map {
      "invoice": $reference,
      "currency": $currency,
      "expected": $expected,
      "paid": $paid,
      "balance": $balance,
      "status": if ($balance = 0) then "paid"
                else if ($balance lt 0) then "overpaid"
                else if (empty($matched)) then "unpaid"
                else "underpaid"
    }
  },
  "unmatchedPayments": array {
    for $payment in $payments
    let $invoice := $invoices[@id = $payment?invoice]
    where empty($invoice[@currency = $payment?currency])
    order by $payment?id
    return map {
      "payment": $payment?id,
      "invoice": $payment?invoice,
      "currency": $payment?currency,
      "amount": xs:decimal($payment?amount),
      "reason": if (empty($invoice)) then "unknown-invoice" else "currency-mismatch"
    }
  }
}

Run the Community example

Extract the ZIP and run the command in the directory containing Example.csproj. The project prints a JSON report and includes expected-report.json for comparison. You can supply two file paths after -- to use your own inputs with the same structure.

Shell
dotnet run -c Release

Download example project ZIP · C# · .NET 8 · Community

Read the result

Payment P-04 has a known invoice reference but the wrong currency. P-05 refers to an unknown invoice. Both appear under unmatchedPayments. Use this list to review exceptions before taking any accounting action.

InvoicePaid (EUR)Balance (EUR)Status
INV-1001120.000.00Paid in two instalments
INV-100250.0039.90Partially paid
INV-10030.0042.50Unpaid
INV-100432.00−2.00Overpaid

Sign in

Choose how you would like to sign in.

Email + password Google Microsoft
Create accountForgot password?