1

I can grab sub elements without an issue, but if i try top level element it fails, i was wondering if anyone knew the best way to grab the value for the ID element

This works to grab sub elements

AUTHENTICATOR_NAME=$(echo "$RESPONSE2" | jq '.[] | .profile.authenticatorName')
 

This does not work for grabbing the value of "id" at a higher level, it returns "jq: error (at :1): Cannot index array with string "id"

FACTOR_ID=$(echo "$RESPONSE2" | jq -r '.id')
 [
 {
 "id": "xxxxxxxxxxx1111111111",
 "factorType": "webauthn",
 "provider": "FIDO",
 "vendorName": "FIDO",
 "status": "ACTIVE",
 "created": "2024年01月31日T19:38:50.000Z",
 "lastVerified": "2024年02月27日T13:28:19.000Z",
 "lastUpdated": "2024年01月31日T19:38:50.000Z",
 "profile": {
 "credentialId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 "appId": null,
 "version": null,
 "authenticatorName": "YubiKey 5",
 "presetPinAvailable": null,
 "fulfillmentProvider": null
asked Feb 27, 2024 at 16:13

1 Answer 1

1

The error is explicit: you're trying to use "id" to access an element of an array.

In the first case you use '.[] | .profile.authenticatorName' which is equivalent to "map each element of the array to its sub property '.profile.authenticatorName'"

In the second case you use '.id' which is "extract the 'id' property from the input".

What you probably meant to use is '.[] | .id' which is "map each element of the array to its property 'id'"

answered Feb 27, 2024 at 16:21
Sign up to request clarification or add additional context in comments.

2 Comments

Note that the shorter .[].profile.authenticatorName and .[].id could have been used (in the same way that .profile.authenticatorName was used instead of .profile | .authenticatorName).
True! I just don't use jq that often to remember its syntax

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.