JSON Processing with jq in Shell

Level 5196 keystrokes

jq is a command-line JSON processor. Filters read a nested value, count array elements and select the names of users older than 26; without -r, strings are printed with their quotes.

Shell code

# Sample JSON: a users array with two objects
1json='{"users":[{"name":"Alice","age":30},{"name":"Bob","age":25}]}'
# Path to a nested value: name of the first user -> "Alice"
2echo "$json" | jq '.users[0].name'
# | passes the array to length -> 2
3echo "$json" | jq '.users | length'
# .users[] emits each user; select keeps those with age > 26 -> "Alice"
4echo "$json" | jq '.users[] | select(.age > 26) | .name'

Output

"Alice"
2
"Alice"
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • "× 20
  • '× 8
  • .× 6
  • |× 6
  • :× 5
  • ,× 3
  • [× 3
  • ]× 3
  • {× 3
  • }× 3

More Shell examples at level 5

← Shell typing practice