jq · lesson 3 of 32 · Getting started

Array index & slice

Index arrays with [n] (zero-based; negative numbers count from the end) and slice them with [from:to] (the end is exclusive).

Examples

Example 1First, last and a range

Document

{ "langs": ["rust", "go", "c", "zig", "lua"] }

Query

.langs[0], .langs[-1], .langs[1:3]
  1. .langs[0] The first element (index 0) → "rust".
  2. .langs[-1] A negative index counts from the end → the last element, "lua".
  3. .langs[1:3] A slice from index 1 up to, but not including, 3 → ["go","c"].

Result

"rust"
"lua"
["go","c"]

Try it in the playground

Good to know

  • Slices work on strings too, and either end may be left out: .[:2] or .[2:].

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.