---
id: HS0065
title: "Typed jsonParsed transactions"
url: https://carbium.sh/library/howsolana/typed-jsonparsed-transactions
collection: How to use Solana
category: History and parsing
revision: 1
updated: 2026-11-22
author: Carbium
code: MIT (LICENSE in the zip); text: all rights reserved, Carbium Corporation
---

# Typed jsonParsed transactions

One TypeScript file fetches three recent System program transactions in jsonParsed, narrows every instruction once, decodes system transfers and token transferChecked into typed records, and prints them with a count of instructions no decoder covers.

## What kit types and what stays open

With the `jsonParsed` encoding the node decodes instructions for the programs it has a parser for, such as the System program and the two token programs, and returns each one as JSON. `@solana/kit` types the envelope of the reply: the slot, the fee, the `meta` block, the account keys, the version. The payload under each instruction is `parsed.info`, and its fields change with the instruction type, so it reaches your code as open JSON.

The usual fix is an interface and a cast, `info as TransferInfo`. The compiler goes quiet and the type is a guess: a multisig transfer carries `multisigAuthority` where a plain one carries `authority`, and a node release can add or rename a field. The file here keeps the value as `unknown` until a check has proven each field, and the checks live in one function per program and instruction type.

The shape is a decoder table. A key such as `system:transfer` maps to a function that takes the `info` object and returns a typed record or `null`. Nothing outside the table touches `info`.

## The two instruction shapes

A message lists its instructions in one of two shapes, and one array can hold both.

| Shape | Keys that identify it | Where you meet it |
|---|---|---|
| Parsed | `program`, `programId`, `parsed.type`, `parsed.info` | System transfers, token transfers and other programs the node can parse |
| Partially decoded | `programId`, `accounts`, `data` | Any program without a parser, such as a swap router |

The `program` field holds a name such as `system`, `spl-token` or `spl-token-2022`, and the address sits in `programId`. The table key joins that name with `parsed.type`, which gives `spl-token:transferChecked`. A partially decoded instruction has no `parsed` key, so the narrowing step returns `null` for it and it lands in the undecoded count. The same two shapes appear inside `meta.innerInstructions`, so the same `decode` function reads inner instructions when you walk them.

## The code

Install `@solana/kit` at 8.6.0, export `CARBIUM_KEY` and run the file with Node 24, which strips the types on load. It takes no argument. It lists the three newest signatures of the System program, which is busy enough to give a fresh sample on every run.

`main.ts` (run: `node main.ts`)

```ts
// HS0065 · Typed jsonParsed transactions
// https://carbium.sh/library/howsolana/typed-jsonparsed-transactions
// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.
// SPDX-License-Identifier: MIT

import { address, createDefaultRpcTransport, createSolanaRpcFromTransport, type Signature } from "@solana/kit";

const key = process.env.CARBIUM_KEY;
if (!key) {
  console.error("Set CARBIUM_KEY to your Carbium API key.");
  process.exit(1);
}
const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";
const rpc = createSolanaRpcFromTransport(
  createDefaultRpcTransport({ url, headers: { "x-api-key": key } }),
);
const commitment = "confirmed" as const;

type Rec = Record<string, unknown>;
type Decoded =
  | { kind: "system.transfer"; source: string; destination: string; lamports: bigint }
  | { kind: "token.transferChecked"; source: string; destination: string; mint: string; authority: string; amount: bigint; decimals: number };
type Decoder = (info: Rec) => Decoded | null;

const isRecord = (v: unknown): v is Rec => typeof v === "object" && v !== null && !Array.isArray(v);

// kit delivers lamports as bigint and tokenAmount.amount as a decimal string; only the number branch is for raw JSON.
function toBig(v: unknown): bigint | null {
  if (typeof v === "bigint") return v;
  if (typeof v === "number" && Number.isSafeInteger(v)) return BigInt(v);
  return typeof v === "string" && /^\d+$/.test(v) ? BigInt(v) : null;
}
const systemTransfer: Decoder = ({ source, destination, lamports }) => {
  const amount = toBig(lamports);
  return typeof source === "string" && typeof destination === "string" && amount !== null
    ? { kind: "system.transfer", source, destination, lamports: amount } : null;
};
const tokenTransferChecked: Decoder = (info) => {
  const { source, destination, mint, tokenAmount } = info;
  const authority = info.authority ?? info.multisigAuthority;
  if (!isRecord(tokenAmount)) return null;
  const amount = toBig(tokenAmount.amount);
  const { decimals } = tokenAmount;
  return typeof source === "string" && typeof destination === "string" && typeof mint === "string" &&
    typeof authority === "string" && typeof decimals === "number" && amount !== null
    ? { kind: "token.transferChecked", source, destination, mint, authority, amount, decimals } : null;
};
const decoders: Partial<Record<string, Decoder>> = {
  "system:transfer": systemTransfer,
  "spl-token:transferChecked": tokenTransferChecked,
  "spl-token-2022:transferChecked": tokenTransferChecked,
};

// Narrow a parsed instruction once, then hand its info to the table.
function decode(ix: unknown): Decoded | null {
  if (!isRecord(ix)) return null;
  const { program, parsed } = ix;
  if (typeof program !== "string" || !isRecord(parsed)) return null;
  const { type, info } = parsed;
  if (typeof type !== "string" || !isRecord(info)) return null;
  return decoders[`${program}:${type}`]?.(info) ?? null;
}

const sigs: Signature[] = [];
try {
  const page = await rpc
    .getSignaturesForAddress(address("11111111111111111111111111111111"), { limit: 3, commitment })
    .send();
  for (const entry of page) sigs.push(entry.signature);
} catch (e) {
  console.error("getSignaturesForAddress failed:", e);
}

const reads = await Promise.allSettled(sigs.map((sig) =>
  rpc.getTransaction(sig, { commitment, encoding: "jsonParsed", maxSupportedTransactionVersion: 1 }).send()));
let decoded = 0, undecoded = 0;
reads.forEach((read, i) => {
  const sig = String(sigs[i]).slice(0, 12);
  if (read.status === "rejected") { console.error(`${sig} failed:`, read.reason); return; }
  if (read.value === null) { console.log(`${sig} null`); return; }
  console.log(`${sig} slot ${read.value.slot}`);
  const ixs: unknown = read.value.transaction.message.instructions;
  for (const ix of Array.isArray(ixs) ? ixs : []) {
    const row = decode(ix);
    if (row === null) { undecoded++; continue; }
    decoded++;
    console.log("  ", JSON.stringify(row, (_k, v) => (typeof v === "bigint" ? v.toString() : v)));
  }
});
console.log(`decoded ${decoded}, left undecoded ${undecoded}`);
```

The three `getTransaction` calls go out together and are settled one by one, so a failed read prints its reason and the others still decode. Every call asks for `confirmed`. The config passes `maxSupportedTransactionVersion: 1`, which a v1 transaction needs: with the value absent or 0, [the node answers -32015](https://carbium.sh/library/howsolana/transaction-version-error-32015) for it. kit types that value as `'legacy' | 0 | 1`, so the literal 1 compiles without a cast, and a config object held in a variable would need `as const`.

A `null` result prints as `null`. `getSignaturesForAddress` pages reach below the validator's ledger floor, so an older signature from that list can come back `null` from `getTransaction`. The newest signatures rarely do, and the branch covers the ones that do.

Each decoded instruction prints as one JSON line with its `kind` and fields, bigint values written as strings. The last line gives two counts: instructions decoded and instructions left undecoded.

The signature list and `getTransaction` cost [50 cells each](https://carbium.sh/docs/rpc). The list runs at 0.24, 1.2, 100 and 500 req/s on Free, Dev, Business and Pro, and `getTransaction` runs at 0.24, 1.2, 5 and 25 req/s, so the three parallel fetches meet the tighter limit. A call over its rate can come back as [429](https://carbium.sh/docs/limits), and the file prints that as a failed read.

## Adding a decoder

1. Fetch one real transaction that holds the instruction and read its `parsed.info` as it comes.
2. Add a member to the `Decoded` union with a new `kind` and the fields you want.
3. Write a function of type `Decoder` that checks each field with `typeof` or `isRecord` and returns the record, or `null` when one check fails.
4. Add a table entry under `program:type`.

Because `Decoded` is a union tagged by `kind`, a `switch` over `kind` in your own code gets an exhaustive check when it ends in a `default` that assigns `kind` to a variable of type `never`, or when the function declares its return type. A new member that the switch forgets then shows up as a type error there. A bare switch compiles without error when it misses a member.

`isRecord` is the one type predicate in the file. A predicate that asserts a whole interface can lie, because the compiler trusts it. `isRecord` checks a single thing, that the value is a non-null object that is not an array, and claims nothing beyond that, so it is safe. Each field then gets its own check at the point of use.

## Numbers in info

kit's response transformer upcasts integers in a `getTransaction` reply to `bigint`, and that includes the values inside `info`, apart from a short allow-list such as token `decimals`. So `lamports` on a system transfer arrives as a `bigint`. The `number` branch in `toBig` covers a transport built without that transformer, where a raw `JSON.parse` hands you a double. It accepts a number only when `Number.isSafeInteger` holds, since a larger one may already be rounded.

| Field | Arrives as | Decoder reads it with |
|---|---|---|
| `info.lamports` on a system transfer | `bigint` through kit | `toBig`, kept as `bigint` |
| `info.tokenAmount.amount` | decimal string | `toBig`, kept as `bigint` |
| `info.tokenAmount.decimals` | `number`, on kit's allow-list | a `typeof` check |

Both amounts stay in base units. Divide a token amount by 10 to the power of `decimals`, and lamports by 10^9, only when a person reads the value, as in [BigInt lamport amounts](https://carbium.sh/library/howsolana/bigint-lamport-amounts). The plain `transfer` instruction carries no mint or decimals in `info`, which is why [Transfer vs TransferChecked decoder](https://carbium.sh/library/howsolana/transfer-vs-transferchecked-decoder) treats it on its own. This table decodes `transferChecked` only.

## Where it ends

The table covers two instruction types. Everything else counts as undecoded: parsed instructions from other programs and every partially decoded one. The count tells you when a decoder is worth adding.

The file reads top-level instructions. Inner instructions use the same two shapes under `meta.innerInstructions`, and passing them through `decode` is a short loop to add.

A failed transaction still lists its instructions, so `decode` can return a transfer that never moved funds. Check `meta.err` first when a record feeds a balance or a report.

The node's parser decides which programs come back parsed and how `info` is laid out. After a node release, a renamed field shows up as a rise in the undecoded count, because each decoder returns `null` on any failed check.

The sample comes from `getSignaturesForAddress`, which reaches back 30 days at most, so older transactions cannot be found through it.

## When it goes wrong

| What goes wrong | What to do |
|---|---|
| -32015 on a transaction, with no result. | The call left out `maxSupportedTransactionVersion` or set it to 0, and the transaction is v1. Pass `maxSupportedTransactionVersion: 1` with the `jsonParsed` encoding. |
| getTransaction answers null for a signature the list returned. | The list reaches below the validator's ledger floor, so an older signature can have no transaction to read. Handle `null` and take newer signatures. |
| Every instruction lands in the undecoded count. | The table key does not match. `program` holds a name such as `spl-token` and the address sits in `programId`; `parsed.type` is case sensitive. Print the joined key for one instruction and compare. |
| Some reads fail with 429 when the three fetches run together. | `getTransaction` has a lower rate than the signature list. Space the reads, or fetch one at a time on a plan with a low `getTransaction` rate. |
| TypeScript error TS18046: 'parsed' is of type 'unknown'. | The value has not been narrowed. Run it through `isRecord` first, then read its fields into constants and check each one. |
| HTTP 401 with UNKNOWN_KEY. | The key did not reach the door whole. Check that `CARBIUM_KEY` holds the complete key and goes in the `x-api-key` header. |

## Questions

### Why not type info with an interface?

A cast checks nothing. A multisig transfer, a changed field name or another program under the same key all pass the compiler and fail at run time. The decoder checks every field and returns `null` when one is wrong.

### Does jsonParsed need maxSupportedTransactionVersion?

Yes. Pass 1. With the value absent or 0, a v1 transaction or a block holding one answers -32015.

### Is lamports a number or a bigint in the reply?

Through kit's default RPC it is a `bigint`. The `number` branch in `toBig` keeps the decoder correct for raw JSON from a transport without kit's response upcast.

### How do I decode inner instructions?

They sit in `meta.innerInstructions`, each group holding an `instructions` array in the same two shapes. Loop over them and call `decode` on each item.

## Related

- [Transfer vs TransferChecked decoder](https://carbium.sh/library/howsolana/transfer-vs-transferchecked-decoder) (How to use Solana, HS0047)
- [Token transfer volume report](https://carbium.sh/library/usecase/token-transfer-volume-report) (Use cases, UC0040)
- [carbium.sh/docs/rpc](https://carbium.sh/docs/rpc)
- [carbium.sh/docs/limits](https://carbium.sh/docs/limits)
- [carbium.sh/docs/errors](https://carbium.sh/docs/errors)
- [carbium.sh/docs/auth](https://carbium.sh/docs/auth)
- [carbium.sh/docs/endpoints](https://carbium.sh/docs/endpoints)
