0

I have the following JSON and try to filter it with JMESPath:

{
  "loadingExperience": {
    "id": "https://www.examle.com/a/",
    "metrics": {
      "CUMULATIVE_LAYOUT_SHIFT_SCORE": {
        "percentile": 2,
        "distributions": [
          {
            "min": 0,
            "max": 10,
            "proportion": 0.9659427443237902
          },
          {
            "min": 10,
            "max": 25,
            "proportion": 0.030108588351431376
          },
          {
            "min": 25,
            "proportion": 0.0039486673247778855
          }
        ],
        "category": "FAST"
      },
      "EXPERIMENTAL_INTERACTION_TO_NEXT_PAINT": {
        "percentile": 101,
        "distributions": [
          {
            "min": 0,
            "max": 200,
            "proportion": 0.90151826015593
          },
          {
            "min": 200,
            "max": 500,
            "proportion": 0.08329913828477642
          },
          {
            "min": 500,
            "proportion": 0.015182601559294217
          }
        ],
        "category": "FAST"
      }
    }
    }
}

My expected output is the following, and the requirement is not to use names of certain metrics in JMESPath expression:

[
"CUMULATIVE_LAYOUT_SHIFT_SCORE": "FAST",
"EXPERIMENTAL_INTERACTION_TO_NEXT_PAINT": "FAST"
]

With *[].metrics[]|[0] I get the full list of metrics with all values:

{
  "CUMULATIVE_LAYOUT_SHIFT_SCORE": {
    "percentile": 2,
    "distributions": [
      {
        "min": 0,
        "max": 10,
        "proportion": 0.9659427443237902
      },
      {
        "min": 10,
        "max": 25,
        "proportion": 0.030108588351431376
      },
      {
        "min": 25,
        "proportion": 0.0039486673247778855
      }
    ],
    "category": "FAST"
  },
  "EXPERIMENTAL_INTERACTION_TO_NEXT_PAINT": {
    "percentile": 101,
    "distributions": [
      {
        "min": 0,
        "max": 200,
        "proportion": 0.90151826015593
      },
      {
        "min": 200,
        "max": 500,
        "proportion": 0.08329913828477642
      },
      {
        "min": 500,
        "proportion": 0.015182601559294217
      }
    ],
    "category": "FAST"
  }
}

With *[].metrics[]|[0].*[].category - only values of category

[
  "FAST",
  "FAST"
]

The solution seems to be very close, I tried multiple syntaxes, but still have no clue how to get the names of metrics. I even don't found any example for such task - all examples I found are about getting values, not labels.

Evgeniy
  • 2,337
  • 2
  • 28
  • 68
  • You cannot do this is JMESPath, addressing dynamic keys either requires a `.*`, but makes you loose the key, or use `keys(@)`, but then your loose the values. Also, your required JSON Is invalid, you can either have an array `["a","b","c"]` or a hash `{"a":"b","c":"d"}`, but you cannot have a mix with keys in an array. – β.εηοιτ.βε Jun 27 '23 at 19:30
  • And on top of that you cannot use dynamic keys to reconstruct a hash in JMESPath either. – β.εηοιτ.βε Jun 27 '23 at 19:34

0 Answers0