>_ IzzyLab

Hands-on task

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

jq: rebuild objects

medium~5 min

/root/products.json is an array of objects with id and price. Build a new JSON array where each element has only two fields: id and price_with_tax = price * 1.2 (the original price field must not be kept). Put the result in /root/taxed.json.

What you'll practise:
  • jq
  • object construction
  • arithmetic in jq
  • working with arrays
  • json

Topic breakdown

In jq, a filter not only extracts data but also builds new structures: the constructor {...} assembles an object from selected fields, and a field's value can be any computed expression, including arithmetic over the original fields. The key idea is understanding the difference between a stream of values and an array: the iteration .[] expands an array into a stream of individual elements, while outer square brackets around the filter collect that stream back into a JSON array. This is how an array of input objects turns into an array of rebuilt ones.

What's checked

  1. each element has id and price_with_tax = price*1.2

What it looks like