Reputation: 494
How can I find recursively first key with given name using jq?
Suppose I have JSON structure:
{
"firstKey": {
"secondKey": {
"model": {
"name": {
....
}
}
}
}
}
Is there any way how to tell jq to return me first found json object with key name for example in this case "model"? So it returns:
"model": {
"name:" {
....
}
}
Upvotes: 0
Views: 175
Reputation: 116780
To select just the first, use first
:
first(.. | objects | select(has("model")))
or if minimizing keystrokes is a goal:
first(..|select(.model?))
Or, if your input has more than one top-level JSON document, and you only want at most one from the bunch:
jq -n 'first(inputs|..|select(.model?))'
Upvotes: 2