Skip to content
IC Reactor

jsonToString

jsonToString(value): string

Defined in: core/src/utils/helper.ts:488

Writes a value as indented JSON text, to show or log what a canister call returned. JSON.stringify throws on a bigint and writes a principal and a blob in forms nobody reads, so the values a Reactor returns are written as a DisplayReactor shows them:

  • a bigint as its decimal digits, in quotes: "100000000";
  • a Principal as its text, "aaaaa-aa", where JSON.stringify writes {"__principal__":"aaaaa-aa"};
  • a Uint8Array (a blob) as lowercase hex without 0x, "0a0b", where JSON.stringify writes an object keyed by index, {"0":10,"1":11};
  • any other typed array (a vec nat16, vec int64, …) as an array of its numbers, a bigint among them as its digits.

Everything else is written as JSON.stringify(value, null, 2) writes it. The text is for people: it does not say which strings were a bigint, a principal or bytes, so it does not parse back into the value.

unknown

The value to write, such as a call’s result.

string

The value as JSON, indented by two spaces.

import { jsonToString } from "@ic-reactor/core"
import { Principal } from "@icp-sdk/core/principal"
jsonToString({
owner: Principal.fromText("aaaaa-aa"),
subaccount: [new Uint8Array([1, 2])],
amount: 5n,
})
// {
// "owner": "aaaaa-aa",
// "subaccount": [
// "0102"
// ],
// "amount": "5"
// }