isJSON({ a: 12, b: [1, 2, 3] }); // false (string required)isJSON({ a: 12, b: [1, 2, 3] }, true); // true (plain object accepted)isJSON([1, 2, 3], true); // false (arrays are not plain objects)isJSON(new Date(), true); // false (Date is not a plain object)isJSON(new Map(), true); // false
Only objects whose prototype is Object.prototype (or null) qualify.
Arrays, class instances, Date, Map, etc. are rejected.
isJSON.strict — accept any valid JSON token
The default isJSON only accepts object/array shapes (a deliberate v3
choice — most callers want "is this a JSON payload"). For full RFC 8259
validity (scalars at the top level), use strict:
isJSON(str, passObjects?) and isJSON.strict(str) keep the same shape
and the same require('is-json') / default-import ergonomics. Behaviour
changes that may affect you:
Scenario
v2
v3
isJSON('[1,2,3]')
false (regex bug)
true
isJSON('{"a":"}"}')
false (regex bug)
true
isJSON('{"msg":"hello world"}')
brittle
true
isJSON.strict({a:1})
true
true (preserved)
isJSON.strict('null')
null (falsy)
true
isJSON.strict(123)
true (parse coercion)
false
isJSON({...}, true) accepting class instances
The regex-based default validator from v2 has been replaced with a
JSON.parse-based check, eliminating false positives and false negatives
around braces in strings, whitespace-in-values, and nested arrays.