Skip to content

isJSONValue

Checks whether a value is composed entirely of JSON-representable types: string, finite number, boolean, null, arrays of JSON values, or plain objects of JSON values. undefined, functions, symbols, NaN/Infinity, and non-plain objects (Date, Map, Set, class instances…) all return false, since none survive a real JSON.stringify / JSON.parse round-trip unchanged. Circular references also return false for the same reason, instead of recursing forever.

Available since v3.0.3

import { isJSONValue } from '@helpers4/guard';
// or, from the all-in-one package (same code, one install):
import { isJSONValue } from 'helpers4/guard';
isJSONValue(value: unknown): boolean
ParameterTypeDescription
valueunknownThe value to check

boolean — true if the value is a valid JSON value

Validate a value before sending it as JSON

Section titled “Validate a value before sending it as JSON”

Recursively checks that a value only contains JSON-representable types.

isJSONValue({ a: [1, 'two', null, { b: true }] })
// => true
isJSONValue({ a: new Date() })
// => false

View source on GitHub