by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    glenveegee
    @glenveegee
    Looks like @jamesls was planning something along those lines too at some point. This feature is on a branch in the jmespath.js repo: https://github.com/jmespath/jmespath.js/compare/jep-11
    glenveegee
    @glenveegee
    @mathpaquette are you using the javascript version by any chance?
    glenveegee
    @glenveegee
    @mathpaquette @jamesls FWIW I've added a JEP that could help. jmespath/jmespath.jep#6
    Mathieu Paquette
    @mathpaquette
    @glenveegee yes Im using JS version
    glenveegee
    @glenveegee
    Cool it's your lucky day (depending how you look at it). I rewrote jmespath.js in typescript. It includes a generic way to create custom functions. So as it's 100% spec compliant you should be able to drop it in without issues (:fingerscrossed) and then just register any function you need to deal with your specific case.
    Here's an example of it in use:
    I think the flatMapValues function would help your case
    Mathieu Paquette
    @mathpaquette
    @glenveegee thanks, Ill certainly give a try
    glenveegee
    @glenveegee
    Cool. Forgot to mention it's in NPM https://www.npmjs.com/package/@metrichor/jmespath
    Mathieu Paquette
    @mathpaquette
    awesome thanks
    Mathieu Paquette
    @mathpaquette
    @glenveegee I think jmespath is missing something like this. https://stedolan.github.io/jq/manual/#Advancedfeatures
    Brandon Max
    @bmax
    hello
    I'm so happy this is still active

    I'm stuck on a little expression here. I'm trying to check if an array of strings contains a specific string, if so, return true. here's what I have so far but this returns false even if the item contains the string:
    metadata.customer.items[*].description | contains(@,`ham`)

    so that first expression might return ['hamburger', 'salad'] and that still returns false.

    Brandon Max
    @bmax
    is there like a loop expression
    Brandon Max
    @bmax
    I ended up going with metadata.customer.items[*].description | !contains(toLowerCase(join(` `,@)),`Ham`) but would like to know if there's a better way
    glenveegee
    @glenveegee
    Hey @bmax . There's no toLowerCase in the spec so I assume you're using a version of JMESPath that's been extended a little. Here's something that's using the original spec but demonstrates an alternative expression :
    contains(map(&contains(@, `bur`), @), `true`)
    I'd have another map/pipe there to convert to lowercase if the function was in the spec.
    Brandon Max
    @bmax
    thanks @glenveegee -- I am using jmespath-plus. What does the & in &contains do?
    glenveegee
    @glenveegee
    & indicates an expression reference which is basically just another JMESPath expression that will be applied to everything in the array. In other words the map() function will apply the JMESPath expression contains(@, 'bur') (indicated by the &) to every member in @
    glenveegee
    @glenveegee
    @bmax out of interest, what are the functions that are only available in jmespath-plus that you find useful?
    Brandon Max
    @bmax
    @glenveegee to lower case mostly haha
    trim/split aren't awful either
    glenveegee
    @glenveegee
    I proposed a JEP for split a few days ago. Perhaps we need some more for trim and lower/uppercase jmespath/jmespath.jep#3
    As it happens I've done my own thing regarding custom functions (see posts above). JMESPath is only as useful as the spec and when simple things become hard, I find it discourages usage of an otherwise fantastic tool.
    Brandon Max
    @bmax
    @glenveegee awesome. I gave it a thumbs up, how do proposals work? Do we need to get 10 people to sign a petition :P
    @glenveegee yeah the syntax is pretty crazy and sometimes confusing. I'm impressed I got far enough to use contains haha
    glenveegee
    @glenveegee
    There's currently no precedent other than the jmespath.jep repo. I defer to @jamesls on what constitutes a worthy proposal
    I'm also not the first. The lovely folks at gorillastack have made an attempt too https://gorillastack-jmespath.netlify.app/proposals
    Brandon Max
    @bmax
    @glenveegee one quick last question for ya
    what's the difference between using ' and ` : (metadata.olo_order.merchant_id | @ == '273677') or (metadata.olo_order.merchant_id | @ == 273677)
    oh. I see. one actually an int and one is a string.
    Brandon Max
    @bmax
    is there a toString(@) or something to get passed that?
    Mathieu Paquette
    @mathpaquette
    glenveegee
    @glenveegee
    @bmax my apologies. I should have used the back tick in the expression above. it's jmespath syntax to interpret the contents as literal JSON values. jmespath tries to do the right thing and convert it to the correct type. There is indeed a to_string() function. More on that here: https://jmespath.org/specification.html#to-string
    glenveegee
    @glenveegee

    @mathpaquette glad you got sorted in the end although now I'm really interested in the visualisation tool you're working on. I'm building a visualisation tool too and I settled on JMESPath to help with any/all JSON response transformations. It's also the reason why I rewrote JMESPath in typescript and extended it with the registerFunction function so I could write custom functions to do this. I've thus far managed to extend it with a host of dataviz related functions including :

    mean, mode, median, toFixed, formatNumber, uniq, divide, split, entries, format, flatMapValues, toUpperCase, toLowerCase, groupBy, combine

    With these extended functions a JMESPath expression would now look like this to remap the key into every value:

    flatMapValues(@)[*].merge([1], {category: [0]})

    I'm serious about the visualisation tool though. Please tell me more.

    bharat19th
    @bharat19th
    Hi Guys, I am very new JMESPATH but now i need to use JMESPATH in ansible playbook..
    here is json i have

    {
    "facts_hash": {
    "lib": "/usr/share/fdi/facts:/opt/extension/facts",
    "interfaces": "eno1,eno2,eno3,eno4,eno5,eno6,ens3f0,ens3f1,lo",
    "ipaddress_eno1": "192.168.101.135",
    "macaddress_eno1": "08:f1:ea:6d:03:3c",
    "netmask_eno1": "255.255.255.0",
    "mtu_eno1": "1500",
    "macaddress_eno2": "08:f1:ea:6d:03:3d",
    "mtu_eno2": "1500",
    "macaddress_eno3": "08:f1:ea:6d:03:3e",
    "mtu_eno3": "1500",
    "macaddress_eno4": "08:f1:ea:6d:03:3f",
    "mtu_eno4": "1500",
    "macaddress_eno5": "b8:83:03:81:4b:20",
    "mtu_eno5": "1500",
    "macaddress_eno6": "b8:83:03:81:4b:21",
    "mtu_eno6": "1500",
    "macaddress_ens3f0": "b8:83:03:84:d5:1c",
    "mtu_ens3f0": "1500",
    "macaddress_ens3f1": "b8:83:03:84:d5:1d",

    },
    "name": "daisy-joni-selitto-quinto",
    "id": 269
    }

    in this i want to filter startwith macaddress_
    and use the list in next task
    but i am not able to do this
    can some please help
    i mean startwith macaddress_..value
    glenveegee
    @glenveegee

    @bharat19th I'm afraid it can't be done with the current spec. You can either evaluate keys or values but not items. There are JEP proposals to this effect but it's not certain if or when they will be discussed.

    In the meantime you may want to add support for one or both of these proposals since JEPs are only ever considered if there is a real world example. CC @jamesls

    flatMapValues() proposal:
    jmespath/jmespath.jep#6

    entries() proposal:
    jmespath/jmespath.jep#2

    Nic Mendoza
    @nicmendoza_gitlab

    Hey everybody, I'm new to JMESPath and trying to create a means for my application's users to define parameterized JMESPath expressions. The approach I conceived of initially is to include a property on the JSON document my code supplies to the search function which includes the parameters. The issue I've run into is that once you're deep into a projection, you don't seem to be able to access properties from ancestor nodes in the document. This may just be a limitation of JMESPath, but I'm hoping this group's experience might help me out of a corner.

    Take the following document:

    { 
      "report_parameters": {
        "category": 1
      },
      "user_docs": [
        { 
          "records": [
            {
              "category": 1, 
              "label": "pants"
            },
            {
              "category": 2,
              "label": "shirts"
            }
          ]
        },
        {
          "records": [
            {
              "category": 1, 
              "label": "shoes"
            }
          ]
        }
      ]
    }

    Searching against that document, I can use this expression to return only items where the category property has the value 1 (which is my goal here):
    user_docs[].records[?category == ``1``][]

    What I'd like to do is use report_paramaters.category to filter user_docs, like this, which doesn't work:
    user_docs[].records[][?category == report_parameters.category]

    I also thought maybe pipes would solve this. This works:
    user_docs[].records[] | [?category == ``2``]

    But this doesn't (for what I imagine are the same reasons):
    user_docs[].records[] | [?category == report_parameters.category]

    Nic Mendoza
    @nicmendoza_gitlab
    Poking around a bit more, seems like I'd want to be able to access the root/parent of a node, which is a missing feature. I switched over to @glenveegee 's TS implementation yesterday to leverage the ability to add custom functions. Does anyone know of a JS/TS implementation that offers an out-of-the-box solution for accessing the root/parent node?
    Nic Mendoza
    @nicmendoza_gitlab
    Just found this guy and going to give it a try: https://github.com/daz-is/jmespath.js. It claims to have custom functions and root access. The creator also has a second project called jmespath-plus which has that plus some more. Not sure why there are two separate projects... https://github.com/daz-is/jmespath-plus
    Nic Mendoza
    @nicmendoza_gitlab
    jmespath-plus solved my problem right out of the box with $ to access the root node!