Learn jq

A filter language: a program is a pipeline that streams JSON values through small filters.

The jq cheat sheet

Getting started

  1. Identity & fields A jq program is a filter: it takes the input document and produces zero or more outputs. . is the simplest…
  2. Nesting & missing keys Chain fields to walk into nested objects. Asking for a key that doesn't exist is not an error — you get…
  3. Array index & slice Index arrays with [n] (zero-based; negative numbers count from the end) and slice them with [from:to] (the…
  4. Iterate & collect .[] turns an array into a stream of its elements — one output each. Wrap any filter in [ … ] to collect a…

Pipes & building output

  1. Pipes | feeds each output of the filter on its left into the filter on its right. It is how small filters combine…
  2. Building objects { … } builds a new object. {name} is shorthand for {name: .name}, and a value can be any expression — wrap…
  3. Strings & interpolation "…\(expr)…" inserts the result of any filter into a string. Combine it with join, ascii_downcase, sub and…

Filtering & conditions

  1. select & comparisons select(condition) lets a value through only when the condition is true — it is jq's way to filter.
  2. and / or / not Combine conditions with and and or, and invert one with not.
  3. if / elif / else if … then … elif … else … end picks one branch per input. Every condition and branch is itself a filter.
  4. Testing text & arrays test, startswith, endswith and contains return booleans, so they slot straight into select.
  5. Missing values: // and empty a // b gives a unless it is null, false or produces nothing — then it gives b. empty produces no output at…

Transforming

  1. map & select together map(f) runs f on every element and collects the results into a new array. Combine it with select to filter…
  2. Updating values Assignment operators produce a modified copy of the input: = sets a value, |= updates it from its old value,…
  3. Working with objects keys lists the key names, has(k) tests for one, a + b merges two objects (the right side wins), and…
  4. Types & conversion type names a value's type, and tostring, tonumber, tojson and fromjson convert between types.

Aggregation

  1. Counting & math Arrays have length, add, min and max, and the usual arithmetic works: + - * / %.
  2. Sort, group & unique sort_by(key) orders an array, group_by(key) buckets its elements into arrays, and unique drops duplicates.…
  3. reduce & foreach reduce stream as $x (start; update) folds a stream into a single value. foreach is the same, but emits the…
  4. limit, first & range limit(n; stream) and first(stream) stop as soon as they have enough, instead of computing everything. range…

Tables & lookups

  1. CSV & TSV @csv and @tsv write an array as one row of text. Build the row with [ … ], one row per result. When the last…
  2. Records to a table Most data is an array of objects. List the columns once, in a variable, and use that one list for the header…
  3. Quoting & escaping Values with commas, quotes or line breaks need care. @csv puts every string in double quotes and doubles any…
  4. IN IN(…) asks whether its input is one of several values and answers true or false, so it belongs inside…
  5. INDEX & JOIN INDEX(f) turns an array into an object keyed by what f gives for each element: a lookup table. INDEX(source;…

Variables & functions

  1. Variables expr as $name | … binds a value for the rest of the pipeline — handy when you need it twice. A pattern on…
  2. Your own functions def name(params): body; defines a reusable filter. A parameter written $n receives a value; a plain name…
  3. Recursion: .., paths, walk .. visits every value in the document, at any depth. Pair it with a type filter such as numbers or strings.…

Event streams

  1. tostream tostream flattens any value into events: [path, value] for every leaf, and a one-element [path] event that…
  2. fromstream & truncate_stream fromstream(events) builds values back from events: it gives one value each time an event closes a top-level…

Errors

  1. try, catch & ? Some filters fail on some inputs — tonumber on "two", for instance. try f catch g recovers with a fallback,…

Reference

  1. jq here vs jq 1.7 jsonquery runs jq programs with jaq, a Rust re-implementation of jq. Everyday filters behave the same, but a…

About jsonquery

Query, format and learn JSON with jq, JSONPath, JMESPath and JSON Pointer. It is the engine of the desktop app, run in your browser.

Engine
The desktop app’s, compiled to WebAssembly
Licence
MIT (the engine and the app)
Source
GitHub
Contact
The contact form (private)
Public issues
GitHub
Legal
Privacy policy

Known limitations: a file over 10 MB opens read-only and one over 512 MB does not open here; the desktop app has no such limit.