jq in Jsontron

Every expression of the keywords (context, test, let, and a message that starts with ") is a jq program. Every implementation evaluates it with jqlite, a portable profile of jq 1.8 implemented natively in each language, so an expression gives the same result in .NET, TypeScript and Python.

What an expression sees

Name Value
. the instance in context and in schema-level let; the context node in test, message and rule-level let
$root the whole document being validated, in every expression, at every schema location
$name the let bindings in scope; see let

An expression can bind its own variables (. as $order | ...) and define functions (def total: ...;).

Truthy tests

A test is truthy when it produces at least one value, and none of its values is false or null. So:

Test On Truthy
.total > 0 {"total": 5} yes
.discount {"discount": 0} yes: 0 is a value, neither false nor null
.discount {} no: null
.items[] \| .qty > 0 {"items": [{"qty": 1}, {"qty": 0}]} no: one of its values is false
.items[] {"items": []} no: no value
empty anything no

A test that produces several values holds only when all of them do, which makes .items[] | .qty > 0 a test that every item has a positive quantity. To require one value at least, wrap it: any(.items[]; .qty > 0).

The portable profile

A schema author can rely on this part of jq in every implementation; the suite file tests/jq-profile.json tests it.

  • Paths and iteration: ., .a, .a.b, .["a"], .[n], slices, .[], .., and the optional ?.
  • Operators: |, ,, arithmetic, comparisons, and, or, not, //.
  • Construction: arrays, objects, string interpolation "\(...)".
  • Control: if/elif/else, try/catch, reduce, foreach, ... as $x, destructuring, def.
  • Builtins: length, keys, has, in, type, select, empty, error, map, map_values, to_entries, from_entries, with_entries, add, any, all, range, first, last, limit, unique, unique_by, group_by, sort, sort_by, min, max, min_by, max_by, contains, inside, index, indices, IN, INDEX, tostring, tonumber, tojson, fromjson, startswith, endswith, ltrimstr, rtrimstr, split, join, ascii_downcase, ascii_upcase, test, match, capture, sub, gsub, floor, ceil, round, sqrt, abs, paths, getpath, flatten, reverse, not, isempty, values, nulls, strings, numbers, booleans, arrays, objects, scalars.

Outside the profile, and not to be relied on:

  • input, output and environment: input, inputs, debug, stderr, input_filename, $ENV, env, halt, halt_error, $__loc__, modules, now and the date functions;
  • regular expression features outside jqlite's flavor: lookaround, backreferences and inline flags such as (?i) (pass flags as the second argument instead, as in test("draft"; "i")); \d, \w and \s match ASCII only;
  • numbers that a double cannot hold exactly, and the exact text tojson gives a non-integer number;
  • the text of jq's error messages.

The jqlite language guide describes the profile in full, with its differences from jq 1.8.

When an expression is wrong

Every expression is compiled on its own when the schema is built, before any document is validated. A syntax error, an unknown function or an unknown label fails the build, with the error type of the host library: see Building schemas in the section of your language.

A $name that is neither $root, nor a binding in scope, nor bound by the expression itself is not a build error: jqlite treats it as a variable of the host, so it fails when the expression is evaluated.

An error raised while evaluating a let, a context or a test (indexing a number, error("..."), an undefined variable) stops the rule set: the failures found so far are dropped, and the schema object reports one validation error carrying jq's error text. Use ?, // or try where data may be missing or of another type:

{ "assert": "(.total? // 0) >= 0" }

An error in a message does not stop anything: the failure is reported with an empty text.