# types-json

> Type checking for JSON values

Latest version **5.0.0** (published 2026-03-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install types-json
pnpm add types-json
yarn add types-json
bun add types-json
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2026-03-10 |
| First published | 2020-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^24.0.0 \|\| ^26.0.0 |
| Dependencies | 2 |
| Unpacked size | 34.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Connor White |
| Maintainers | bconnorwhite |
| Keywords | json, object, value, array, typescript, types, type, checking, guard, guards |

## Links

- npm: https://www.npmjs.com/package/types-json
- Repository: https://github.com/bconnorwhite/types-json
- Homepage: https://github.bconnorwhite.com/types-json#readme
- Issues: https://github.com/bconnorwhite/types-json/issues
- npm.io page: https://npm.io/package/types-json

## Dependencies (2)

- [zod](https://npm.io/package/zod.md) ^3.20.2
- [is-zod](https://npm.io/package/is-zod.md) ^2.0.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 5.0.0 (latest) — 2026-03-10
- 4.0.1 — 2023-01-29
- 4.0.0 — 2023-01-27
- 3.2.0 — 2023-01-27
- 3.1.2 — 2023-01-24
- 3.1.1 — 2023-01-24
- 3.1.0 — 2023-01-24
- 3.0.0 — 2023-01-11
- 2.0.2 — 2023-01-11
- 2.0.1 — 2023-01-10
- 2.0.0 — 2023-01-10
- 1.2.2 — 2020-10-04
- 1.2.1 — 2020-09-19
- 1.2.0 — 2020-09-10
- 1.1.0 — 2020-09-10
- … 3 more at https://npm.io/package/types-json/versions

## README

<!--BEGIN HEADER-->
<div id="top" align="center">
  <h1>types-json</h1>
  <a href="https://npmjs.com/package/types-json">
    <img alt="NPM" src="https://img.shields.io/npm/v/types-json.svg">
  </a>
  <a href="https://github.com/bconnorwhite/types-json">
    <img alt="TypeScript" src="https://img.shields.io/github/languages/top/bconnorwhite/types-json.svg">
  </a>
</div>

<br />

<blockquote align="center">Type checking for JSON values.</blockquote>

---
<!--END HEADER-->

This package uses [zod](https://www.npmjs.com/package/zod) to type check JSON values.

It includes type guards for each of the JSON types, as well as parse functions and corresponding types.

<!-- BEGIN INSTALLATION -->
## Installation

<details open>
  <summary>
    <a href="https://www.npmjs.com/package/types-json">
      <img src="https://img.shields.io/badge/npm-CB3837?logo=npm&logoColor=white" alt="NPM" />
    </a>
  </summary>

```sh
npm install types-json
```

</details>

<details>
  <summary>
    <a href="https://yarnpkg.com/package/types-json">
      <img src="https://img.shields.io/badge/yarn-2C8EBB?logo=yarn&logoColor=white" alt="Yarn" />
    </a>
  </summary>

```sh
yarn add types-json
```

</details>

<details>
  <summary>
    <img src="https://img.shields.io/badge/pnpm-F69220?logo=pnpm&logoColor=white" alt="PNPM" />
  </summary>

```sh
pnpm add types-json
```

</details>

<details>
  <summary>
    <img src="https://img.shields.io/badge/bun-EE81C3?logo=bun&logoColor=white" alt="Bun" />
  </summary>

```sh
bun add types-json
```

</details>
<!-- END INSTALLATION -->

## Contents

- [Installation](#installation)
- [Usage](#usage)
  - [JSONValue](#jsonvalue)
  - [JSONObject](#jsonobject)
  - [JSONArray](#jsonarray)
  - [JSONPrimitive](#jsonprimitive)
  - [JSONOrderable](#jsonorderable)
  - [String](#string)
  - [Number](#number)
  - [Boolean](#boolean)
  - [Null](#null)
  - [Undefined](#undefined)

## Usage

### JSONValue

When using `isJSONValue`, values which cannot be parsed or serialized properly using `JSON.parse` and `JSON.stringify` return false.

Similarly, when using `parseJSONValue`, invalid values return `undefined`.

Finally, a zod schema provided representing the `JSONValue` type.

```ts
import { isJSONValue, parseJSONValue, jsonValueSchema } from "types-json";

isJSONValue(undefined); // false
isJSONValue(null); // true
isJSONValue(NaN); // false
isJSONValue(Infinity); // false
isJSONValue([1, 2, 3]); // true
isJSONValue([1, 2, () => 3]); // false
```

Optional types are also provided:

```ts
import { isOptionalJSONValue, parseOptionalJSONValue, optionalJSONValueSchema } from "types-json";

isOptionalJSONValue(undefined); // true
isOptionalJSONValue({ a: 1, b: undefined }); // true
```

### JSONObject

JSONObject is a type guard and parse function for objects which can be parsed and serialized using `JSON.parse` and `JSON.stringify`.

```ts
import {
  isJSONObject,
  parseJSONObject,
  jsonObjectSchema
} from "types-json";

isJSONObject({ foo: "bar" }); // true
isJSONObject({ foo: () => "bar" }); // false
parseJSONObject({ foo: "bar" }); // true
parseJSONObject({ foo: () => "bar" }); // undefined
```

Optional types are also provided. `OptionalJSONObject` includes `undefined`, while `NestedOptionalJSONObject` only allows for `undefined` values nested within the object:

```ts
import {
  isOptionalJSONObject,
  parseOptionalJSONObject,
  optionalJSONObjectSchema,
  isNestedOptionalJSONObject,
  parseNestedOptionalJSONObject,
  nestedOptionalJSONObjectSchema
} from "types-json";

isOptionalJSONObject(undefined); // true
isOptionalJSONObject({ a: 1, b: undefined }); // true

isNestedOptionalJSONObject(undefined); // false
isNestedOptionalJSONObject({ a: 1, b: undefined }); // true
```

### JSONArray

```ts
import {
  isJSONArray,
  parseJSONArray,
  jsonArraySchema,
} from "types-json";

isJSONArray([1]); // true
isJSONArray([1, () => 2]); // false
parseJSONArray([1]); // []
parseJSONArray([1, () => 2]); // undefined
```

Optional types are also provided. `OptionalJSONArray` includes `undefined`, while `NestedOptionalJSONArray` only allows for `undefined` values nested within objects inside the array. Note that `undefined` values nested within arrays are not allowed, as they are not valid JSON:

```ts
import {
  isOptionalJSONArray,
  parseOptionalJSONArray,
  optionalJSONArraySchema,
  isNestedOptionalJSONArray,
  parseNestedOptionalJSONArray,
  nestedOptionalJSONArraySchema
} from "types-json";

isOptionalJSONArray(undefined); // true
isOptionalJSONArray([1, undefined]); // false
isOptionalJSONArray([1, { a: 1, b: undefined }]); // true

isNestedOptionalJSONArray(undefined); // false
isNestedOptionalJSONArray([1, undefined]); // false
isNestedOptionalJSONArray([1, { a: 1, b: undefined }]); // true
```

### JSONPrimitive

```ts
import {
  isJSONPrimitive,
  parseJSONPrimitive,
  jsonPrimitiveSchema
} from "types-json";

isJSONPrimitive("foo"); // true
isJSONPrimitive(1); // true
isJSONPrimitive(true); // true
isJSONPrimitive(null); // true
isJSONPrimitive(undefined); // false
isJSONPrimitive({}); // false
```

Optional types are also provided:

```ts
import {
  isOptionalJSONPrimitive,
  optionalJSONPrimitiveSchema
} from "types-json";

isOptionalJSONPrimitive("foo"); // true
isOptionalJSONPrimitive(1); // true
isOptionalJSONPrimitive(true); // true
isOptionalJSONPrimitive(null); // true
isOptionalJSONPrimitive(undefined); // true
isOptionalJSONPrimitive({}); // false
```

### JSONOrderable

`JSONOrderable` is a type for values which can be reasonably compared using operators such as `>`, `<`, `>=`, and `<=`:

```ts
import {
  isJSONOrderable,
  parseJSONOrderable,
  jsonOrderableSchema
} from "types-json";

isJSONOrderable("foo"); // true
isJSONOrderable(1); // true
isJSONOrderable(true); // false
isJSONOrderable(null); // false
isJSONOrderable(undefined); // false
isJSONOrderable({}); // false
isJSONOrderable([]); // false
```

Optional types are also provided:

```ts
import {
  isOptionalJSONOrderable,
  optionalJSONOrderableSchema
} from "types-json";

isOptionalJSONOrderable("foo"); // true
isOptionalJSONOrderable(1); // true
isOptionalJSONOrderable(true); // false
isOptionalJSONOrderable(null); // false
isOptionalJSONOrderable(undefined); // true
isOptionalJSONOrderable({}); // false
isOptionalJSONOrderable([]); // false
```

### String

```ts
import {
  isString,
  parseString,
  stringSchema
} from "types-json";

isString("foo"); // true
isString(1); // undefined
parseString("foo"); // "foo"
parseString(1); // undefined
```

Optional types are also provided:

```ts
import {
  isOptionalString,
  optionalStringSchema
} from "types-json";

isOptionalString("foo"); // true
isOptionalString(undefined); // true
```

### Number

```ts
import {
  isNumber,
  parseNumber,
  numberSchema
} from "types-json";

isNumber(1); // true
isNumber("1"); // undefined
parseNumber(1); // true
parseNumber("1"); // undefined
```

Optional types are also provided:

```ts
import {
  isOptionalNumber,
  optionalNumberSchema
} from "types-json";

isOptionalNumber(1); // true
isOptionalNumber(undefined); // true
```

### Boolean

```ts
import {
  isBoolean,
  parseBoolean,
  booleanSchema
} from "types-json";

isBoolean(true); // true
isBoolean("true"); // undefined
parseBoolean(true); // true
parseBoolean("true"); // undefined
```

Optional types are also provided:

```ts
import {
  isOptionalBoolean,
  optionalBooleanSchema
} from "types-json";

isOptionalBoolean(true); // true
isOptionalBoolean(undefined); // true
```

### Null

```ts
import {
  isNull,
  parseNull,
  nullSchema
};

isNull(null); // true
isNull("not null"); // undefined
parseNull(null); // null
parseNull("not null"); // undefined
```

Optional types are also provided:

```ts
import {
  isOptionalNull,
  optionalNullSchema
} from "types-json";

isOptionalNull(null); // true
isOptionalNull(undefined); // true
```

### Undefined

Finally, an `isUndefined` type guard is provided:

```ts
import { isUndefined } from "types-json";

isUndefined(undefined); // true
isUndefined("string"); // false
```

<br />

## Related Packages

- [types-pkg-json](https://www.npmjs.com/package/types-pkg-json): Type checking for package.json
- [types-tsconfig](https://www.npmjs.com/package/types-tsconfig): Type checking for tsconfig.json
- [types-eslintrc](https://www.npmjs.com/package/types-eslintrc): Type checking for .eslintrc.json

<!--BEGIN FOOTER-->
<h2 id="dependencies">Dependencies<a href="https://www.npmjs.com/package/types-json?activeTab=dependencies"><img align="right" alt="dependencies" src="https://img.shields.io/librariesio/release/npm/types-json.svg"></a></h2>

- [is-zod](https://www.npmjs.com/package/is-zod): Typeguard to check if a value matches a zod schema
- [zod](https://www.npmjs.com/package/zod): TypeScript-first schema declaration and validation library with static type inference

<h2 id="license">License <a href="https://opensource.org/licenses/MIT"><img align="right" alt="license" src="https://img.shields.io/npm/l/types-json.svg"></a></h2>

[MIT](https://opensource.org/licenses/MIT) - _MIT License_
<!--END FOOTER-->

---
_Source: https://npm.io/package/types-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
