Skip to content

isIJson

Checks whether a value is a string holding I-JSON (RFC 7493, the Internet JSON message format): strict JSON that every JSON implementation reads the same way.

On top of strict JSON (see isJSON), I-JSON rejects what JSON.parse silently accepts but loses or reads inconsistently:

  • duplicate keys in an object (JSON.parse keeps the last one; other parsers don’t)
  • a string or key holding a lone surrogate ("\uD800") or a Unicode noncharacter ("\uFFFF")
  • a number beyond the range of a double (1e400), more precise than a double holds (more than 17 significant digits, or a non-zero value that rounds to 0), or an integer outside +/-(2^53 - 1)

RFC 7493 also recommends (but doesn’t require) an object or an array at the top level — this guard accepts any value there.

Available since v3.1.3

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

value is string — true if value is a string holding I-JSON

Strict JSON that every implementation reads the same way.

isIJson('{"id":1,"name":"app"}') // => true

JSON.parse keeps the last duplicate key and rounds big integers; I-JSON rejects both.

isIJson('{"id":1,"id":2}')  // => false
isIJson('9007199254740993') // => false

View source on GitHub