NURLNURL registrynurl-lang.org →

← all packages

nq

owner @Hindurable

Install

[dependencies]
nq = "^0.1.1"

Versions

Dependencies (latest)

None.

nq — a tiny jq-lite JSON query tool

nq is the kind of utility every developer keeps within reach: it reads JSON from stdin (or a file), applies a small jq-like filter, and prints the result — pretty by default, compact or raw on request.

It lives in the NURL registry (it is not part of the core stdlib). nq parses flags with the stdlib std/args and leans on the shipped stdlib/ext/json for parsing and printing.

nurlpkg install nq

Usage

nq [OPTIONS] [FILTER]

FILTER is a jq-lite path; the default is ., the whole document. Input is read from stdin unless --file is given.

OptionMeaning
-c, --compactcompact, single-line output
-r, --rawraw output for string results (no surrounding quotes)
-f, --file Fread JSON from file F instead of stdin
-h, --helpshow help

Filter grammar

filter := path [ '|' func ]
func   := keys | length
FilterResult
.the whole document
.user.namenavigate objects/arrays (use .0 for index 0, [0] also works)
.items[]iterate an array or object — one result per line
.items[].idproject a field out of each iterated element
. | keysobject keys (or array indices) as an array
. | lengthlength of an array / object / string

A missing path yields null (like jq). Iterating a scalar, or applying keys/length to a value of the wrong type, is an error (exit code 1).

Examples

$ echo '{"user":{"name":"Ada","age":36},"tags":["x","y","z"]}' | nq .user
{
  "name": "Ada",
  "age": 36
}

$ echo '{"user":{"name":"Ada"}}' | nq -r .user.name
Ada

$ echo '{"items":[{"id":1},{"id":2},{"id":3}]}' | nq '.items[].id'
1
2
3

$ echo '{"a":1,"b":2,"c":3}' | nq -c '. | keys'
["a","b","c"]

$ echo '["x","y","z"]' | nq '. | length'
3

Quality

nq is leak-clean under AddressSanitizer / LeakSanitizer across every code path (navigation, iteration, the keys/length terminals, and the error/parse-failure branches). The end-to-end install-and-run loop is covered by tools/nurlpkg/test-install-tool.sh.