JMESPath advanced multi select and contains queries

60 Views Asked by At

Given this JSON below I want to make two jmespath queries:

  1. Select all the blocks which have the keys XXXclass and YYYclass (the strings "XXXclass" and "YYYclass" will be in the query)
  2. Select all the blocks in "item" which ".offer.type" contains the string "-interest"

I made multiples tests in https://jmespath.org/, read the tutorials, examples and specs but can't find how to make the two queries above. Thanks in advance

{
  "items": [
    {
      "id": "1",
      "type": "A",
      "XXXclass": {
        "offer": {
          "price": "15",
          "type": "dddd"
        },
        "label": "myLabel"
      },
      "YYYclass": {
        "offer": {
          "price": "75",
          "type": "gggg"
        },
        "label": "myOtherLabel"
      },
      "ZZZclass": {
        "offer": {
          "price": "125",
          "type": "dddd"
        },
        "label": "myLabelTwo"
      }
    },
    {
      "id": "2",
      "type": "B",
      "XXX-class": {
        "offer": {
          "price": "95",
          "type": "dddd"
        },
        "label": "myLabelthree"
      }
    }
  ]
}
1

There are 1 best solutions below

0
Vladimir Botka On

The below query

items[].[XXXclass, YYYclass]|[]

gives

[
  {
    "offer": {
      "price": "15",
      "type": "dddd"
    },
    "label": "myLabel"
  },
  {
    "offer": {
      "price": "75",
      "type": "gggg"
    },
    "label": "myOtherLabel"
  }
]