VDL Plugin SDK

isJSON

Import from @varavel/vdl-plugin-sdk/utils/predicates.

function isJSON(value): value is string;

Checks if a given value is a valid JSON string.

A valid JSON string is one that can be successfully parsed using JSON.parse(). According to JSON specifications, valid JSON can represent:

  • Objects (with string keys and valid JSON values)
  • Arrays (containing valid JSON values)
  • Strings
  • Numbers
  • Booleans
  • null

String values like "null", "true", "false", and numeric strings (e.g., "42") are considered valid JSON and will return true.

This function serves as a type guard in TypeScript, narrowing the type of the argument to string.

Parameters

ParameterTypeDescription
valueunknownThe value to check.

Returns

value is string

Returns true if value is a valid JSON string, else false.

Example

isJSON('{"name":"John","age":30}'); // true
isJSON('[1,2,3]'); // true
isJSON('true'); // true
isJSON('invalid json'); // false
isJSON({ name: 'John' }); // false (not a string)
isJSON(null); // false (not a string)