Skip to content

isJsonc

Checks whether a value is a string holding valid JSONC — JSON with comments and trailing commas, the dialect of tsconfig.json and VS Code’s settings.json, also known as HuJSON or JWCC (JSON With Commas and Comments).

Strict JSON is valid JSONC too, so this is true for everything isJSON accepts, plus line and block comments, trailing commas before } and ], and a leading byte order mark. JSON5 syntax isn’t JSONC — use isJson5 for that (parse from @helpers4/json reads both).

Available since v3.1.3

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

value is string — true if value is a string holding JSON or JSONC

Comments and trailing commas are fine; strict JSON is too.

isJsonc('{"a": 1, // note\n}') // => true
isJsonc('{"a": 1}')             // => true

Single quotes and unquoted keys are rejected.

isJsonc("{'a': 1}") // => false

View source on GitHub