Skip to content

isJSON

Checks whether a value is a string containing valid, parseable JSON text.

Distinct from isJSONValue, which checks an already-parsed runtime value’s shape — this checks a string before you parse it. Pairs naturally with @helpers4/object’s safeJsonParse, which is the safe-parse counterpart once you know the string is valid — this helper reuses it internally rather than re-implementing the parse/catch itself.

Available since v3.0.3

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

value is string — true if value is a string and JSON.parse succeeds on it

Validates that a string is parseable JSON before calling JSON.parse.

isJSON('{"a":1}')
// => true
isJSON('not json')
// => false

View source on GitHub