I want jq to return the index of an alement in an array based on a search.
Using the below array I call jq to find the value "FooBar"
of a key "text"
:
jq '.arr[] | .[3].text | tostring | select(contains("FooBar") )' < file.json
The result is:
"FooBar"
But what I actually want is the index of the most outer array "arr"
that this text:FooBar
pair is nested in, which is 0 in this case.
Can this e achieved in jq?
{
"arr": [
[
"create",
"w71",
"rwt.widgets.Label",
{
"parent": "w68",
"style": "1",
"bounds": "2",
"tabIndex": -1,
"background": "ww",
"font": "test",
"text": "FooBar",
"alignment": "right"
}
],
[
"create",
"w72",
"rwt.widgets.Label",
{
"parent": "w68",
"style": "22",
"bounds": "1",
"tabIndex": -1,
"foreground": "null",
"background": "1",
"font": "2",
"text": "55",
"alignment": "right"
}
]
]
}
You can first convert the elements in the array to entries, this way both the key and the value are present in the output:
jq '.arr | to_entries'
Give the result where the key
is present in the output:
[
{
"key": 0,
"value": [
"create",
"w71",
"rwt.widgets.Label",
{
"parent": "w68",
"style": "1",
"bounds": "2",
"tabIndex": -1,
"background": "ww",
"font": "test",
"text": "FooBar",
"alignment": "right"
}
]
},
{
"key": 1,
"value": [
"create",
"w72",
"rwt.widgets.Label",
{
"parent": "w68",
"style": "22",
"bounds": "1",
"tabIndex": -1,
"foreground": "null",
"background": "1",
"font": "2",
"text": "55",
"alignment": "right"
}
]
}
]
Performing your filtering and returning the index then becomes fairly trivial:
jq '.arr | to_entries | .[] | select(.value[3].text | contains("FooBar")) | .key' <test.json
Here's a solution that does not depend on the assumption that the object of interest has a fixed position within the array:
.arr
| map( .[] | objects | .text )
| index("FooBar")
More robustly:
.arr
| map( first(.[] | objects) // null | .text )
| index("FooBar")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With