How do I get the first index of a query that contains multiple matches? I am trying to get the value of the first key called ELEMENT in this query:
```
{
"ELEMENT": "FADC6B14-0369-4FF4-9ADC-A5E0F5C6D30D",
"type": "XCUIElementTypeStaticText",
"label": "this.com"
}
{
"ELEMENT": "CC89DD73-AB90-495E-A90B-74722C56DD46",
"type": "XCUIElementTypeStaticText",
"label": "this.com"
}
{
"ELEMENT": "B888ADA6-4209-44C4-BCB8-F5174312D102",
"type": "XCUIElementTypeStaticText",
"label": "this.com"
}
{
"ELEMENT": "9A6E77C7-E93D-41DE-9163-2CB60B8DD2FB",
"type": "XCUIElementTypeStaticText",
"label": "this.com"
}
```
Here is the cURL that produces this result:
curl -X POST $JSON_HEADER -d "{\"using\":\"partial link text\",\"value\":\"label=this.com\"}" $DEVICE_URL/session/$SESSION_ID/elements | jq -r '.value[] | select(.label=="this.com")'
I was thinking it could be label[0]=="this.com"
but I get the message Cannot index string with number
It looks like .value contains an array; if so, then the jq query that matches your description would be:
.value | map( select(.label=="this.com")) | .[0]
If there is no match, this will produce null
, so you might want to make adjustments accordingly.
If you want the value of .ELEMENT in the first match, you could consider:
.value | map( select(.label=="this.com")) | .[0] // empty | .ELEMENT
If your jq has first/1
, a more efficient solution is possible:
first( .value[] | select(.label=="this.com") ) | . ELEMENT
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.