Cannot parse output of Server item using jq

Pontoffel_Pock
Pontoffel_Pock
Community Member

When I run "op get item" and pipe it to "jq '.details.fields[]'", if the item is a Login, I see the expected output of fields and their values. However, if the item is a Server, I get "jq: error: Cannot iterate over null". Running the "op get item" without piping to jq results in the expected JSON output in both cases. This leads me to believe that the JSON output from a Server item contains a null somewhere, but visual inspection of the JSON output is inconclusive.


1Password Version: 0.1.1
Extension Version: Not Provided
OS Version: Ubuntu 14.04.5
Sync Type: Not Provided

Comments

  • AGAlumB
    AGAlumB
    1Password Alumni

    @Pontoffel_Pock: Do you have the same problem if you create a new item? I wonder if there's a [null] in some data you saved there previously. We've encountered issues like that before on some platforms.

  • Pontoffel_Pock
    Pontoffel_Pock
    Community Member

    Yes, I have the same problem with a newly created item as well.

  • Pontoffel_Pock
    Pontoffel_Pock
    Community Member

    I just performed an additional test and I'm seeing different results. If I create a Server item in the 1password application on my Mac, I have the null problem. However, if I create a Server item via the web interface, I do NOT have the null problem. Unfortunately, though, the Server item created via the web interface does not return any fields or values at all when piped through jq.

    $ op get item "appserveritem" | jq ".details.fields[]"
    jq: error: Cannot iterate over null
    $

    $ op get item "webserveritem" | jq ".details.fields[]"
    $

    When I look at the JSON output of those two items without piping to jq I see a difference in fields. So the two item creation methods seem to be different.

  • This is due to the difference in structures between a Login item and most other items. The fact that it doesn’t error with the item created from the webapp is a bug with the webapp. Login items have details.fields which is where the interesting fields are located. Most item types have details.sections.fields.

    None of this stuff is publicly documented, sadly. It’s on our very long list of things to do. While it’d be nice to make things more consistent between item types, doing so would come at the cost of compatibility with older apps.

    I hope this helps

    Rick

  • Pontoffel_Pock
    Pontoffel_Pock
    Community Member
    edited October 2017

    Yes, I see the fields label under the sections label, now. That being the case, shouldn't the following work?

    $ op get item "appserveritem" | jq ".details.sections.fields[]"
    jq: error: Cannot index array with string

    Or is this just a case of me not using jq properly? If that's the case, please let me know and I'll do additional jq and JSON research on my own. I realize that the jq utility is not the responsibility of AgileBits.

  • At that point it's just a matter of getting jq to play ball and traverse the tree. I'm not a jq expert by any means but I managed to get it to fetch the username out of a Server item via:

    ./op get item ServerItem | jq -r '.details.sections[]  | select(.title=="") | .fields[] | select(.n=="username") |.v '
    

    So basically get the sections, find the one whose title is "", which is the first one, the primary section, which has usernames. Field the field in there whose n (name, also known as the field identifier) is "username", then output the v (value) of that field.

    I hope this helps.

    Rick

  • Pontoffel_Pock
    Pontoffel_Pock
    Community Member

    That helps tremendously! So it was as I suspected, I need to spend more time getting to understand jq. Thank you for taking the time to share a solution and explain the details. Very much appreciated.

  • You're welcome. There's probably a more efficient way of doing that with jq than what I came up with there. For example the last | could be removed and instead just have it be select().v. I'm sure there are more... but I'm no pro with jq at all. It's a fun little tool to work with though, so I enjoy such challenges to help me figure out how it works.

    Cheers.

    Rick

  • fatso83
    fatso83
    Community Member

    A total plug from me, but if you find it a bit bothersome to learn yet another DSL for stuff you already know well how to do in JS using filter and map, pick_json might be useful :-)

    $ echo '[ { "bar" : 42 } ]' |  pick_json -e "[0].bar > 40" #returns true
    $ echo '[ { "bar" : 42 } ]' |  pick_json -e "map( e => e.bar * 2 )" # returns [84]
    

    It's just plain old js chaining of commands.

  • AGAlumB
    AGAlumB
    1Password Alumni

    Cool! Thanks for sharing that! :) :+1:

  • Thanks @fatso83. I hadn't heard of pick_json.

    Rick

This discussion has been closed.