jmespath.site icon indicating copy to clipboard operation
jmespath.site copied to clipboard

How to query by key?

Open mhubig opened this issue 8 years ago • 5 comments

Hi I could not find the answer to this problem:

Given the following structure, how do I get all the "objects" those keys start with e.g 'block8'?

{
    "prod_data": {
        "block2.row2": {
            "status": "yes",
            "date": "2016-05-09",
            "name": "Karin",
            "time": "06:57:28"
        },
        "block8.row4": {
            "status": "yes",
            "date": "2016-05-18",
            "name": "Isolde",
            "time": "11:22:02"
        },
        "block3.row1": {
            "status": "yes",
            "date": "2016-05-13",
            "name": "Isolde",
            "time": "13:11:59"
        },
        "block8.row2": {
            "status": "yes",
            "date": "2016-05-18",
            "name": "Isolde",
            "time": "11:22:02"
        }
    }
}

mhubig avatar Dec 08 '16 16:12 mhubig

As far as I can tell, this is not possible as there is no partial string matching defined in the specification, only equality, inequality, greater than (or equal to), or less than (or equal to), provided by Filter Expressions.

Additionally, I don't see a way to apply those filters to object keys, only objects in an array.

Partial string matches (perhaps regex?) would be a great addition to this project as would be the ability to filter by object keys.

schlueter avatar Jul 16 '17 17:07 schlueter

@schlueter what about contains, starts_with and ends_with? You can certainly find the keys:

keys(prod_data)[?starts_with(@, 'block8')]

Unfortunately, I don't think there's a way to apply that in a multi-select hash (in part because the multi-select hash demands identifiers), and all transformations of the hash into an array lose either the keys (values(), * or [*]) or the values (keys()). You'd need to add a 'zip' function.

wryun avatar Aug 19 '17 20:08 wryun

I missed those when I was looking. I'm not a regular jmespath user, I usually use jq.

schlueter avatar Aug 22 '17 18:08 schlueter

@mhubig , @wryun , @schlueter - Would you happen to know how to query and obtain all the dates in this case? Would it be "[*].'"block2.row2'".date" ? I am facing challenges providing the right reference to extract date (in this analogy) for a similar use case. I couldn't find any reference in the examples as well.

geverghe avatar Apr 03 '20 16:04 geverghe

Not sure the exact use case of your question @geverghe but to get the dates assuming this payload

{
    "prod_data": {
        "block2.row2": {
            "status": "yes",
            "date": "2016-05-09",
            "name": "Karin",
            "time": "06:57:28"
        },
        "block8.row4": {
            "status": "yes",
            "date": "2016-05-18",
            "name": "Isolde",
            "time": "11:22:02"
        },
        "block3.row1": {
            "status": "yes",
            "date": "2016-05-13",
            "name": "Isolde",
            "time": "13:11:59"
        },
        "block8.row2": {
            "status": "yes",
            "date": "2016-05-18",
            "name": "Isolde",
            "time": "11:22:02"
        }
    }
}

you could probably do something like *.*[date][] to get this output

[
  [
    "2016-05-09"
  ],
  [
    "2016-05-18"
  ],
  [
    "2016-05-13"
  ],
  [
    "2016-05-18"
  ]
]

If your question was around the date for only block2.row2 date it would be something like this *."block2.row2".date according to their identifiers section of the spec to get this output

[
  "2016-05-09"
]

GalexyN avatar Mar 16 '23 20:03 GalexyN