'semantics of map on a sequence of objects in jq

Suppose I have a file fruit.json containing the following lines:

[
  {
    "name": "apple",
    "color": "red",
    "price": 20
  },
  {
    "name": "banana",
    "color": "yellow",
    "price": 15
  },
  {
    "name": "pineapple",
    "color": "orange",
    "price": 53
  }
]

If I do jq '. | map(.)' fruit.json then I get the original data. That's expected. The second . refers to an element in the entire array.

However if I do jq '.[] | map(.)' fruit.json then I get this:

[
  "apple",
  "red",
  20
]
[
  "banana",
  "yellow",
  15
]
[
  "pineapple",
  "orange",
  53
]

Can someone please explain what's going on? Specifically,

  1. The [] after . strips away the brackets from the input array. Do we have a name for the [] operator? The manual seems to treat it as something very basic without definition.
  2. Do we have a name for the resulting thing by appending [] to .? Obviously it's not an object. If we do jq '.[]' fruit.json we can see that it looks very similar to an array. But apparently it behaves quite differently.
  3. Why is it the case that the map function seems to go two levels inside instead of one? This is more obvious if we do jq '.[] | map(. | length)' fruit.json and see that the . inside the map function refers to the value part of an (object) element of the input array.

Thank you all in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source