I'm sure this is very simple, but I do not know how to do it with jq.
I have a JSON like this one
[
{
"id": "109",
"name": "aaa"
},
{
"id": "1098",
"name": [
"bbb",
"ccc"
]
},
{
"id": "2000",
"name": [
"fff",
"ddd"
]
}
]
And I would like to extract the .name
. When name
is an array, I would like to take always the first element.
Do I must create an if
clause, or is there some more direct mode?
As output I would like to have "aaa" "bbb" "fff"
, but I have "aaa" "bbb" "fff" "bbb" "fff"
.
Here is what I have so far:
if .[].name|type == "array" then .[].name[0]? else first(.[].name) end
Output:
"aaa"
"bbb"
"fff"
"bbb"
"fff"
Interactive example https://jqplay.org/s/fOBuwdj6WS