gojq
gojq copied to clipboard
Addition of list mapping
This commit allows map queries on lists. Performing a map query on a list results in a new list, where each element is the element at the queried key of each object in the list that was queried. (mind the word-salad; I couldn't think of a better way to accurately explain it)
Example
JSON
{
"test": [
{
"name": "elgs"
},
{
"name": "enny"
},
{
"name": "sam"
}
]
}
Code
input.Query(test.name) // ["elgs", "enny", "sam"]
Thanks for the code. Just a quick question, would it conflict with the following JSON data semantically?
{
"test": {
"name": ["elgs", "enny", "sam"]
}
}
I just edited the example to remove my commas (which would've caused a syntax error)
I just tested my example and the one you just provided - each outputs the expected results. Semantically, I suppose it does, but I don't think that's a problem; in order to write a query you already have to make an assumption about the structure of the data.
Let me know if this answers your question
An alternative might be to require the query to be test.[*].name
, which is what I was originally going to do, but I decided test.name
was more elegant; If the former syntax was required, it would imply you have two "types" of lists, a list-that-you-are-not-allowed-to-map
list and a list-that-you-can-map
list