JQ Tool

Query and transform JSON using jq syntax.

Usage

The jq tool can read from files, inline JSON, or command output.

From File

{
  "file": "data.json",
  "query": ".items[] | select(.active == true)"
}

From Inline JSON

{
  "json": {"users": [{"name": "Alice"}, {"name": "Bob"}]},
  "query": ".users[].name"
}

From Command

{
  "command": "curl -s https://api.example.com/data",
  "query": ".results[:5]"
}

Parameters

ParameterDescription
queryjq filter expression (required)
fileJSON file path
jsonInline JSON object
commandCommand that outputs JSON

One of file, json, or command must be provided.

jq Syntax

Common jq operations:

ExpressionDescription
.Identity (whole input)
.fieldAccess field
.[]Iterate array
.[0]First element
.[:5]First 5 elements
select(.x == 1)Filter
map(.x)Transform array
keysObject keys
lengthLength
sort_by(.x)Sort by field

Examples

Extract names from array:

{
  "json": [{"name": "Alice"}, {"name": "Bob"}],
  "query": ".[].name"
}

Filter active items:

{
  "file": "items.json",
  "query": ".[] | select(.status == \"active\")"
}

Get API response field:

{
  "command": "curl -s https://api.github.com/repos/user/repo",
  "query": "{name: .name, stars: .stargazers_count}"
}

See Also