Skip to content

Json

const Json: object

Defined in: Data/Json.ts:24

Pure, non-throwing JSON utilities. Wraps runtime JSON parsing and stringifying in typed Result containers.

parse: (text) => Result<SyntaxError, unknown>

Safely parses a JSON string into unknown. Converts thrown exceptions into a Result<SyntaxError, unknown>.

string

Result<SyntaxError, unknown>

Json.parse('{"a": 1}'); // Ok({ a: 1 })
Json.parse('{invalid}'); // Err(SyntaxError)

stringify: (value, replacer?, space?) => Result<TypeError, string>

Safely stringifies a value into a JSON string. Converts thrown exceptions (e.g. circular references) into a Result<TypeError, string>.

unknown

(this, key, value) => any

string | number

Result<TypeError, string>

Json.stringify({ a: 1 }); // Ok('{"a":1}')
import { Json } from "@nlozgachev/pipelined/data";
import { pipe } from "@nlozgachev/pipelined/composition";

const result = pipe(
  Json.parse('{"name":"Alice"}'),
  Result.map((data: any) => data.name)
); // Ok("Alice")