Choosing Between Dyalog ⎕JSON and Manual JSON Encoding
A concise decision guide for when to use Dyalog’s built‑in ⎕JSON facility versus manual JSON conversion in APL code.
20 Jun 2026, 17:48 UTC

Decision and Constraints
If you need to serialize or deserialize JSON data in Dyalog APL, the first decision is whether to rely on the built‑in ⎕JSON system function or to implement manual conversion with primitive functions such as ⎕UCS and ⎕DR. Use ⎕JSON when you are running Dyalog version 14.0 or later, your data consists only of JSON‑representable APL arrays (numbers, characters, nested arrays, and ⎕NULL), and you do not require fine‑grained control over the exact JSON text format. Manual conversion becomes preferable only when you need custom number formatting, want to avoid temporary character vectors for massive homogeneous numeric arrays, or must support interpreters older than version 14.0.
Option Comparison
| Option | Minimum Dyalog Version | Implementation Effort | Control Over Output | Typical Use Case |
|---|---|---|---|---|
⎕JSON | 14.0 | Low | Medium (handles ⎕NULL and nesting automatically) | General‑purpose JSON interchange |
Manual via ⎕UCS/⎕DR | Any | High | Full (you decide number formatting, escaping, whitespace) | Custom formats or extreme‑performance scenarios |
⎕XML | 12.0 | Low | Low (XML‑only) | When XML interchange is required |
Trade‑offs
The built‑in ⎕JSON function yields concise code: a single call encodes an APL array to a JSON character vector, and another call decodes it back. It automatically maps ⎕NULL to JSON null and handles nested structures without extra code. However, it offers limited customization—there is no built‑in pretty‑print option, and you cannot influence how floating‑point numbers are rendered beyond the default representation. It also requires a recent interpreter (≥14.0).
Manual conversion gives you complete control over the generated JSON text. You can, for example, format numbers with a fixed number of decimal places or omit insignificant trailing zeros. This approach can avoid creating intermediate character vectors when processing huge homogeneous numeric arrays, potentially reducing memory pressure. The downside is verbosity: you must write encoding and decoding logic, handle escaping, and manage edge cases such as ⎕NULL yourself, which increases the chance of bugs.
Concrete Implementation and Validation
The following steps illustrate a simple round‑trip using ⎕JSON in a Dyalog session. No special privileges are required; you only need the ability to execute APL expressions.
- Start a Dyalog session (version 14.0 or newer).
- Create a test array that contains numbers, characters, and a
⎕NULLelement: - Encode the array to JSON:
- Decode the JSON back to an APL array:
- Verify that the round‑trip produced an identical array:
- Optional performance check: compare timing for a large numeric matrix using
⎕TIMESPACEagainst a manual⎕UCS-based encoder (not shown here).
data ← (⍳3) ('Hello' 'World') ⎕NULL
json ← ⎕JSON data
The variable json now holds a character vector containing valid JSON.
roundTrip ← ⎕JSON json
identical ← data ≡ roundTrip
If identical is 1, the encoding and decoding succeeded without loss of information.
Limitations and checks: If data contains a function, namespace, or ⎕OR object, step 2 will raise a DOMAIN ERROR. Always validate input or wrap the call in ⎕TRAP to prevent the session from terminating. After decoding, malformed JSON also signals an error; you can test validity by attempting the decode and catching any ERROR.
Practical verification: after obtaining json, you can inspect its first few characters with 5↑json to confirm it begins with a JSON array ([) or object ({) as expected. The round‑trip equality test (data ≡ roundTrip) is the definitive check that the conversion preserved shape and contents.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.