📄 Checks if a value is a valid JSON string.

Info

This function returns true for any syntactically valid JSON string, including objects, arrays, strings, numbers, booleans, and null.

Syntax

TypeScript
import { isJSON } from '@opentf/std';

isJSON(val: unknown): boolean

Parameters

  • val: The value to check.

Returns

true if the string is valid JSON, false otherwise.

Examples

TypeScript
isJSON("{}") //=> true

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

isJSON("[]") //=> true

isJSON("null") //=> true

isJSON("123") //=> true

isJSON("true") //=> true

isJSON('"hello"') //=> true

isJSON({}) //=> false

isJSON(null) //=> false

// Invalid JSON
isJSON("{a: 1}") //=> false (Keys must be double-quoted)

isJSON("") //=> false
Last updated on
Edit this page