>_ IzzyLab

Hands-on task

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

jq: top by field into a new object

medium~7 min

From /root/users.json find the user with the highest number of logins and build a new JSON object for them of the form {"top": "<name>", "logins": <number>}, writing it to /root/top.json.

What you'll practise:
  • jq
  • sort_by
  • object construction
  • extremum selection
  • json

Topic breakdown

Finding the record with the maximum field value is a matter of sorting by key and taking the extreme element: sort_by(...) orders the array in ascending order, so the maximum ends up last (extracted with last, a negative index, or a reverse and the first). From the chosen element you then build a new object: in jq an object is built right in the expression with curly braces, where the right fields of the source element are assigned to the values.

What's checked

  1. top user and object are correct

What it looks like