parse-json-object
Parse a typed JSON object.
Last updated 4 years ago by bconnorwhite .
MIT · Repository · Original npm · Tarball · package.json
$ gnpm install parse-json-object 
SYNC missed versions from official npm registry.

parse-json-object

NPM TypeScript Coverage Status

Parse a typed JSON object.

If I should maintain this repo, please ⭐️ GitHub stars

DM me on Twitter if you have questions or suggestions. Twitter


  • Returns undefined if unable to parse
  • Returns value if successful

Installation

yarn add parse-json-object
npm install parse-json-object
pnpm add parse-json-object

Usage

Types

import {
  parseJSONValue,
  parseJSONObject,
  parseJSONArray,
  parseString
} from "parse-json-object";

parseJSONValue("1"); // 1
parseJSONValue("not valid json"); // undefined

parseJSONObject('{"a": 1}'); // { a: 1 }
parseJSONArray("[1, 2, 3]"); // [1, 2, 3]
parseString('"hello"'); // "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: 1, b: 'hello' }

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); // 1
parse("not a number", isNumber); // undefined

Dependenciesdependencies

  • is-zod: Typeguard to check if a value matches a zod schema
  • types-json: Type checking for JSON values

Dev Dependencies

  • autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.
  • zod: TypeScript-first schema declaration and validation library with static type inference

License license

MIT


Related Packages:

Current Tags

  • 3.0.1                                ...           latest (2 years ago)

12 Versions

  • 3.0.1                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.0.1                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.6                                ...           4 years ago
  • 1.0.5                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (2)

Copyright 2013 - present © cnpmjs.org | Home |