0PricingLogin
Linux Command Line & Bash Scripting Mastery · Lesson

Filtering and Selecting JSON with jq Pipelines

Navigate nested objects and arrays using jq selectors, pipes, and the select filter.

What is jq and Why Use It?

jq is a lightweight, powerful command-line tool for parsing, filtering, and transforming JSON data. It is the sed of JSON — you pipe JSON into it and get structured output back.

  • Pre-installed on most Linux distros or available via apt install jq / brew install jq
  • Works seamlessly in shell pipelines with curl, cat, and other tools
  • Supports filtering, mapping, reduction, and format conversion

The basic invocation is: jq '<filter>' file.json or piped as cat file.json | jq '<filter>'. The filter . (dot) is the identity — it pretty-prints the whole document.

# Pretty-print a JSON file
jq '.' data.json

# Or pipe from curl
curl -s https://api.github.com/users/torvalds | jq '.'

Selecting Object Fields with Dot Notation

To access a field in a JSON object, use dot notation: .fieldName. You can chain selectors to navigate nested objects.

  • .name — top-level field
  • .address.city — nested field
  • ."field-with-dash" — fields with special characters need quotes

If the field does not exist, jq returns null rather than erroring. This makes it safe to use in scripts without extra null-checks for optional fields.

# Given: {"name":"Alice","address":{"city":"Berlin","zip":"10115"}}
echo '{"name":"Alice","address":{"city":"Berlin","zip":"10115"}}' | jq '.name'
# Output: "Alice"

echo '{"name":"Alice","address":{"city":"Berlin","zip":"10115"}}' | jq '.address.city'
# Output: "Berlin"

All lessons in this course

  1. Filtering and Selecting JSON with jq Pipelines
  2. Transforming and Building JSON Objects with jq
  3. Consuming REST APIs with curl and jq Together
  4. Editing YAML Configuration Files with yq
← Back to Linux Command Line & Bash Scripting Mastery