🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

parse-json-object

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-json-object

Parse a typed JSON object

latest
Source
npmnpm
Version
4.0.0
Version published
Weekly downloads
25K
-14.8%
Maintainers
1
Weekly downloads
 
Created
Source

parse-json-object

NPM TypeScript

Parse a typed JSON object.

Installation

NPM
npm install parse-json-object
Yarn
yarn add parse-json-object
PNPM
pnpm add parse-json-object
Bun
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"); // 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

License license

MIT - MIT License

Keywords

safe

FAQs

Package last updated on 11 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts