>_ IzzyLab

Hands-on task

This task is step 2 of the course “JSON, YAML and XML in the terminal”

jq: filter by condition

easy~3 min

/root/users.json contains an array users, where each entry has an active field (true/false). Output the names of only the active users, one per line, and write them to /root/active.txt.

What you'll practise:
  • jq
  • json filtering
  • select
  • raw output
  • array expansion

Topic breakdown

jq is a query language and processor for JSON that works as a pipeline of filters joined by |. An array is expanded into a stream of individual elements with .users[], and the filter select(<condition>) only passes through elements where the condition is true (here the condition is a boolean field of each object). From the selected elements you extract the right field by name (.name), and to print strings without surrounding quotes, jq has a "raw" output mode (-r).

What's checked

  1. active names are correct

What it looks like