Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Jan Walzer
    @jwalzer
    and I just now found out, that: If its a boolean (yes) then I don't need to compare to anything, but can just do an ipfabric.*[].links[?bfd].....
    @oryulin yep, thats how I normally construct this stuff to. This time I had to adjust the existing(and working) query for that new parameter, and thought, it could be so easy. But it was an insightful chat, nevertheless ... thank you
    Oryulin
    @oryulin
    Absolutely @jwalzer, glad I could be of assistance before my work day started.
    Jan Walzer
    @jwalzer
    hehe .. here in Berlin the workday is about to end soon ;) -- but to summarize my understanding: Because of the flattening (that I'm doing nevertheless at the end) my old query worked without the filtering. But because of the .. thing flattening also the filter didn't work, and I had to change to .[]. to avoid that flattening. Right?
    Oryulin
    @oryulin

    Your original query was accurate in my opinion...I think the only problem you had was due to ipfabric.*.links as that would never yield results because the asterisk converts the dict to a list of matching dicts... so we had to deal with that by adding the flattener right after the asterisk:

    ipfabric.*[].links

    The rest was on par I believe (minus of course Ansible's fun yes/no to boolean good times). :)

    You could also pipe it again if that makes it look more visually appealing too, semantics to me.
    ipfabric.* | [].links[?bfd].nodes.h01.interface | []
    Jan Walzer
    @jwalzer
    I have to admit, I'm not yet so natural with the syntax of jmespath and I'll need more practice on that before the semantics are intuitive for me. But at least I could solve that problem, and I'm sure I'll get to the power of jmespath in the future again, before trying uggly things with loops in ansible/jinja
    Oryulin
    @oryulin
    Not that this is the write chat for this statement, but JMESPath > Ansible loops in the sense of speed and efficiency. If you are only data processing/manipulation, I would stay FAR away from Ansible loop as its resource costly/timely. JMESPath can filter hundreds of thousands of list/items in seconds whereas that same processing in Ansible loop...you'll be sitting a while. If you are doing actual work like API calls, then you sort of have to do Ansible loop.
    Jan Walzer
    @jwalzer
    Jep, thats no question at all -- I've seen lots of uggly ansible code with recursive loops down the hierarchy, just to find some objects or keys in data structures, that are possible with a oneliner, exactly like the one above. Luckily, Infrastructure managed with ansible isn't that ofthen so large, that the timing issue really matters, but of course, it's also about complexity and maintainability of the code, and a single, readable oneliner is much better in that metric to
    Oryulin
    @oryulin

    @jwalzer -
    Be sure to use this little nugget with JMESPath and Ansible if you ever attempt to use functions in JMESPath with Ansible such as map, ends_with, et...:

        - name: JMESPath Testing
          vars:
            jmespath_query: >-
              ipfabric.* | [].links[*].nodes.h01.interface | [] | [?ends_with(@,'1')]
          debug:
            var: network | json_query(jmespath_query)

    By all accounts this should work but you find that you get a "type" error:
    In function ends_with(), invalid type for value: swp1, expected one of: ['string'], received: \"unknown

    This is due to Ansible's data types not matching the JSON spec is what I believe is the root cause. To fix it simply add the to_json and from_json syntax before the json_query and profit.

        - name: JMESPath Testing
          vars:
            jmespath_query: >-
              ipfabric.* | [].links[*].nodes.h01.interface | [] | [?ends_with(@,'1')]
          debug:
            var: network | to_json | from_json | json_query(jmespath_query)
    Jan Walzer
    @jwalzer
    this must be quite a "LolWTF!?!?!"-Moment then *g
    and these are the people, telling me, that perl code is uggly
    Christoph
    @cruepprich
    Hi all. I have a question regarding querying time stamps. In the example below I would like to filter the items that are within the last 10 days of the current date. Is there a JMESPath function/feature that allows me to do that?
    So when I run the query on September 16th it should return the first item only (backup b3).
    [{
        "backup": "b3",
        "time_ended": "2020-09-10T14:55:39.840000+00:00"
    }, {
        "backup": "b2",
        "time_ended": "2020-08-25T15:40:48.740000+00:00"
    }, {
        "backup": "b1",
        "time_ended": "2020-07-10T15:40:48.740000+00:00"
    }]
    alonsocamaro
    @alonsocamaro
    Hi everybody
    where can find a jmespath online expression evaluator
    ?
    I find jsonpath ones but not jmespath ones
    more precisely, the one in jmespath.org doesn't always update when fields are updated
    so I'm looking for another one
    Oryulin
    @oryulin

    @alonsocamaro - THe homepage for JMESPath can be used as an online evaluator if desired. Its a real-time test and you can manipulate the search and json structure that will be searched.

    https://jmespath.org/

    I have not ran into an issue where it has "failed" for me thus far unless there is an error in my syntax which is most likely the cause of your not updating. That is one area where it lacks is it doesn't let you know when the syntax has an error so its likely assumed to be a web-based error. If you have a specific scenario/filter feel free to type it here and if I am paying attention will attempt to help.

    Charles Albrecht
    @karillo_gitlab

    I asked this on stackoverflow as well, but thought I'd ask it here to try and get some traction.

    AWS's list-secrets has an odd way of listing version strings as keys:

    {
      "SecretList": [
        {
          "Name": "sandbox_config",
          "Description": "sandbox config file",
          "SecretVersionsToStages": {
            "2895f1a4-bcae-46b2-9a45-4f06f490d8ed": [
              "AWSCURRENT"
            ],
            "336a5030-cd81-4626-a100-1aa68b70b5b1": [
              "AWSPREVIOUS"
            ]
          }
        }
      ]
    }

    I'm trying to figure out a query that will let me pull out the versionkey where AWSCURRENT is in the value array.

    This seems like it's close:

    SecretList[].{Version:SecretVersionsToStages.*[?@==`AWSCURRENT`]|[]|[0] }

    But I'm clearly missing something, since that just spits back AWSCURRENT to me.

    PeiSong
    @naivefun
    Hi, how can I do simple math with jmespath? like length(values(@)) * 2 ?
    glenveegee
    @glenveegee
    Can't be done with the standard jmespath spec. Some "augmented" jmespath libraries add this functionality but not available in all languages
    PeiSong
    @naivefun
    oh I need use custom func to impl myself
    glenveegee
    @glenveegee
    There are libraries out there that provide the ability to add your own custom functions to the existing jmespath spec. I'm afraid this is the only way I have found to make it possible to support the kind of operations I've required in the past.
    I've also created proposals as suggested by the author @jamesls here (https://github.com/jmespath/jmespath.jep) in the hope that they get added to the spec
    Unfortunately @jamesls seems quite busy at the moment as there hasn't been any feedback from him in the last 6 months
    glenveegee
    @glenveegee

    If you could... would you want to be able to do something like this:

    expect(
        search([6.1, 4.2, 6.3], "_groupBy(@, as_lambda('Math.floor'))")
    ).toStrictEqual({
          '4': [4.2],
          '6': [6.1, 6.3],
        });

    or perhaps

    expect(
        search([1, 2], "_flatMap(@, as_lambda('x => [x, x]'))")
    ).toStrictEqual([1, 1, 2, 2]);

    Thoughts?

    glenveegee
    @glenveegee
    Meh... did it anyway. _flatMap, _groupBy and 184 other (fully typed) functions have been built into JMESPath expressions in our https://www.npmjs.com/package/@metrichor/jmespath-plus library. Level-up your JMESPath expressions with the power of lodash functions. Check out https://github.com/nanoporetech/jmespath-plus/releases/tag/v0.3.0 for a complete list of supported lodash functions
    innocuoussoul
    @innocuoussoul
    is it possible to filter expected values so the resulting response is only unique?
    { "subject": [ "Fwd: Please complete your assigned training" ], "labelIds": [ "UNREAD", "CATEGORY_PERSONAL", "Label_6568222846829069483", "INBOX" ] }
    {subject:payload.headers[?name=='Subject'].value,labelIds:labelIds}
    i want to filter UNREAD, CATEGORY_PERSONAL, AND INBOX... so only that unique label remains.
    glenveegee
    @glenveegee
    {subject: payload.headers[?name=='Subject'].value, labelIds:map(&(!contains(['UNREAD', 'CATEGORY_PERSONAL', 'INBOX'], @) && [@]) || [] ,labelIds)[]}
    OR
    {subject: payload.headers[?name=='Subject'].value, labelIds:labelIds[*][(!contains(['UNREAD', 'CATEGORY_PERSONAL', 'INBOX'], @) && @) ||[]][]}
    Moisés Jiménez
    @Mo_Jim_twitter

    Hi everyone, I have a question. Is it possible to use the result of a query to search again? For example, say we have the slightly modified example from jmespath.org

    {
    "locations": [
    {"name": "Seattle", "state": "WA"},
    {"name": "New York", "state": "NY"},
    {"name": "Bellevue", "state": "WA"},
    {"Seattle": "Olympia", "state": "WA"}
    ]
    }

    could this query
    {value: @, key: keys(@)[?ends_with(@, 'Seattle')]} | @.value.[@.key]

    return "Olympia"??

    glenveegee
    @glenveegee
    You should be able to pipe your projection but the query is probably not working in the way you want
    What are you actually trying to do
    Moisés Jiménez
    @Mo_Jim_twitter
    so what I want to do is find all the keys in a dictionary that end with a certain string and then use them to extract the values
    glenveegee
    @glenveegee
    something like this?
    locations[?ends_with(state, 'A')].name
    Moisés Jiménez
    @Mo_Jim_twitter
    yeah this gives me just the key names, same as keys(@)[?ends_with(@, 'Seattle')], now I need to use that to extract the values
    glenveegee
    @glenveegee
    Could you write the result of the query you'd like (what you hope to get out). I'm afraid I still don't follow
    Moisés Jiménez
    @Mo_Jim_twitter
    lets say we have this:
    {"key_a_0": "a", "key_b_0": "b", "key_c_1": "c", "key_d_2": d, "key_e_0": "e"}
    and we do
    {value: @, key: keys(@)[?ends_with(@, '_0')]} | @.value.[@.key]
    I would like the result to be
    "a", "b", "e"
    stiller_leser
    @stiller_leser_twitter
    Is it possible to filter by timestamp? Say I have a timestamp in my json and I would only like to retrieve the entries from the last 5 minutes?
    glenveegee
    @glenveegee
    @Mo_Jim_twitter I don't think that's possible. What language are you coding in?
    @stiller_leser_twitter If it's a formatted date then no. If it's in milliseconds then yes but only if you hard code now time
    What language are you working with?
    glenveegee
    @glenveegee
    @jamesls please, please, please can we look at a proposal for an entries function in the spec so we can get object keys and values in the same execution context.
    This isn't the first time this sort of query has arisen and I'm certain it won't be the last
    stiller_leser
    @stiller_leser_twitter
    Hm ok @glenveegee is there a way to only get the last x entries then? Currently my query looks like this: resources[?involvedObject.apiVersion == v1 && involvedObject.kind == Service && involvedObject.name==my-name].{ firstTimestamp: firstTimestamp, lastTimestamp: lastTimestamp, count: count, reason: reason, message: message }