parse-json-object
Parse a typed JSON object.
Installation
npm install parse-json-object
yarn add parse-json-object
pnpm add parse-json-object
bun add parse-json-object
- Returns
undefined if unable to parse
- Returns value if successful
Usage
Types
import {
parseJSONValue,
parseJSONObject,
parseJSONArray,
parseString
} from "parse-json-object";
parseJSONValue("1");
parseJSONValue("not valid json");
parseJSONObject('{"a": 1}');
parseJSONArray("[1, 2, 3]");
parseString('"hello"');
Additionally, a parse function is provided, which takes a function to validate the parsed value. This can be easily used with zod to validate more complex types:
import { parse } from "parse-json-object";
import z from "zod";
const schema = z.object({
a: z.number(),
b: z.string()
});
parse('{ a: 1, b: "hello" }', schema);
A custom typeguard can also be used:
import { parse } from "parse-json-object";
function isNumber(value: unknown): value is number {
return typeof value === "number";
}
parse("1", isNumber);
parse("not a number", isNumber);
Related Packages:
Dependencies
- is-zod: Typeguard to check if a value matches a zod schema
- types-json: Type checking for JSON values
License 
MIT - MIT License