Expression examples
Identity query (.)
The identity query passes the document through with no mutation. It is the default when neither --query nor -f is given.
Input (config.json):
Stdout:
Useful for normalising a file to nesdit's canonical encoding without changing any values.
Field access and mutation
Set a field
Stdout: {"x":99,"y":2}
Update in-place with |=
|= applies an expression to the current value of the field:
Stdout: {"x":2,"y":2} (incremented by 1)
Chained mutations
Multiple assignments can be chained with |:
Stdout: {"x":10,"y":20}
Deletion
Delete a key
Stdout: {"x":1}
Delete an array element by index
Input:
Stdout: {"items":[10,20,40,50]}
Conditionals
Apply a query only when a condition is met:
Input (env.yaml):
Stdout:
If .env were "staging", the document would pass through unchanged.
String interpolation
Build a string from a field value using jq's \(.field) syntax:
Input:
Stdout:
--arg: inject a shell variable as a string
--arg K=V binds $K in the query as a literal string. Numbers passed this way remain strings.
Input (a.json):
Stdout:
Numbers stay as strings with --arg:
Stdout:
--argjson: inject a shell variable as a typed JSON value
--argjson K=V parses V as JSON and binds it as a typed value. Use this for numbers, booleans, arrays, and objects.
Integer:
Stdout:
Array:
Stdout:
Object:
Stdout:
If the value is not valid JSON, nesdit exits 1 with an error:
--from-file: load a query from a .jq file
For complex or reusable queries, store them in a file:
q.jq:
Input (a.json):
Stdout:
The short form is -f:
Warning
--query and --from-file are mutually exclusive:
--where: apply query only to matching documents
In a multi-document stream, --where restricts the query to documents that satisfy a jq predicate. Other documents pass through unchanged.
Input (stream.yaml):
Stdout:
Stderr (for the skipped document):
$${VAR} escape — literal ${VAR} in a query
nesdit never interpolates shell environment variables. If you need the literal string ${FOO} in a value, escape the $ by doubling it:
Stdout:
The shell environment variable FOO is never read. $${FOO} is always the literal string ${FOO}.
--create-missing: allow queries to create new keys
By default, assigning to a path that does not exist is an error:
Input:
nesdit: error: config.json: query.missing_path: path .b does not exist; use --create-missing to allow path creation
Add --create-missing to let nesdit create intermediate containers:
Stdout: