Unable to get Secret using jq

PeterCharleston
PeterCharleston
Community Member
edited June 2021 in CLI
op get item 'ItemName' --vault 'VaultName' | jq '.details.fields[] |  | select(.designation=="password").value'

jq: error: password/0 is not defined at <top-level>, line 1:
.details.fields[] | select(.designation==password).value
jq: 1 compile error

If i just specify designation i get a result - see attachment


1Password Version: Not Provided
Extension Version: Not Provided
OS Version: Not Provided
Sync Type: Not Provided

Comments

  • Hello @PeterCharleston,

    Thanks for writing in with your jq question.

    It looks like PowerShell is incorrectly parsing your quotation marks. In the error message, you'll notice that the echoed command no longer includes quotes around the word password.

    jq: error: password/0 is not defined at <top-level>, line 1:
    .details.fields[] | select(.designation==password).value
                                            ^~~~~~~~~^
    

    I want to reassure you that your code should work. I re-created this scenario in jqplay (an online sandbox), and you can see that your usage of select() is correct. So, the issue is figuring out how to make PowerShell understand your single- and double-quotes.

    In PowerShell, it turns out that you have to backslash the double-quotes, even when working inside a single-quotes context:

    PS C:\Users\Michael> op get item 'TestItem' --vault 'TestVault' | jq '.details.fields[] | select(.designation == \"password\")'
    {
      "designation": "password",
      "id": "",
      "name": "password",
      "type": "P",
      "value": "test-password-value"
    }
    

    An alternative would also be to put your jq command in a file and pass that to jq, like so:

    # In command.txt:
    
    .details.fields[] | select(.designation == "password")
    
    # In PowerShell:
    
    PS C:\Users\Michael> op get item 'TestItem' --vault 'TestVault' | jq -f command.txt
    {
      "designation": "password",
      "id": "",
      "name": "password",
      "type": "P",
      "value": "test-password-value"
    }
    

    I hope that this helps. Please feel free to write back with any additional questions.

  • PeterCharleston
    PeterCharleston
    Community Member

    Hi Michael,

    that's awesome.
    I tried both ways and works great.
    I think I tried using the escape character in PowerShell before '`' but it didn't work.

    Helped me out a lot.
    Cheers
    Peter

  • ag_ana
    ag_ana
    1Password Alumni

    On behalf of Michael, you are welcome @PeterCharleston! If you have any other questions, please feel free to reach out anytime.

    Have a wonderful day :)

This discussion has been closed.