'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,
- 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. - Do we have a name for the resulting thing by appending
[]to.? Obviously it's not an object. If we dojq '.[]' fruit.jsonwe can see that it looks very similar to an array. But apparently it behaves quite differently. - Why is it the case that the
mapfunction seems to go two levels inside instead of one? This is more obvious if we dojq '.[] | map(. | length)' fruit.jsonand see that the.inside themapfunction 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 |
|---|
