如何使用 jsonpath 为每个数组迭代 return 父值?

How to return parent value for each array iteration with jsonpath?

我正在尝试通过 JSON API.

将一些 json 数据导入 grafana

这是我正在使用的 json 结构的片段:

[
  {
    "origin": "TS",
    "id": "M8C8E02434D442725422CCB337057792F",
    "type": "1.5.1:1",
    "self": "https://metricsourcehost01/uimapiM8C8E02434D442725422CCB337057792F",
    "source": "destinationhost01.our.domain.net",
    "target": "destinationhost01.our.domain.net-0",
    "probe": "cdm",
    "for_computer_system": {
      "id": "14873",
      "self": "https://metricsourcehost01/uimapi/devices/14873",
      "name": "destinationhost01.our.domain.net",
      "ip": "10.1.1.16"
    },
    "for_device": {
      "id": "D4F3D290D787D3FA4E7CD2824BFA6B1C8",
      "self": "https://metricsourcehost01/uimapi/devices/D4F3D290D787D3FA4E7CD2824BFA6B1C8"
    },
    "for_configuration_item": {
      "id": "CCE5006B73554FE7D307C1A355429286A",
      "self": "https://metricsourcehost01/uimapi/TBD/CCE5006B73554FE7D307C1A355429286A",
      "name": "CPU-0",
      "qosName": "QOS_CPU_MULTI_USAGE",
      "description": "Individual CPU Usage",
      "unit": "%"
    },
    "uimMetricDefinition": null,
    "minSampleValue": 61.17,
    "maxSampleValue": 72.78,
    "meanSampleValue": 64.864,
    "sample": [
      {
        "time": "2021-09-02T00:50:32.000Z",
        "timeSinceEpoch": 1630543832,
        "value": 61.17,
        "rate": 60
      },
      {
        "time": "2021-09-02T00:49:32.000Z",
        "timeSinceEpoch": 1630543772,
        "value": 63.52,
        "rate": 60
      },
      {
        "time": "2021-09-02T00:48:32.000Z",
        "timeSinceEpoch": 1630543712,
        "value": 62.79,
        "rate": 60
      },
      {
        "time": "2021-09-02T00:47:32.000Z",
        "timeSinceEpoch": 1630543652,
        "value": 64.06,
        "rate": 60
      },
      {
        "time": "2021-09-02T00:46:32.000Z",
        "timeSinceEpoch": 1630543592,
        "value": 72.78,
        "rate": 60
      }
    ]
  },
  {
    "origin": "TS",
    "id": "M9D90857B9F9BE73EB15912D3314DB2DA",
    "type": "1.5.1:1",
    "self": "https://metricsourcehost01/uimapiM9D90857B9F9BE73EB15912D3314DB2DA",
    "source": "destinationhost01.our.domain.net",
    "target": "destinationhost01.our.domain.net-1",
    "probe": "cdm",
    "for_computer_system": {
      "id": "14873",
      "self": "https://metricsourcehost01/uimapi/devices/14873",
      "name": "destinationhost01.our.domain.net",
      "ip": "10.1.1.16"
    },
    "for_device": {
      "id": "D4F3D290D787D3FA4E7CD2824BFA6B1C8",
      "self": "https://metricsourcehost01/uimapi/devices/D4F3D290D787D3FA4E7CD2824BFA6B1C8"
    },
    "for_configuration_item": {
      "id": "CF1D7A708DD4C6C9D303025AE3D2334AE",
      "self": "https://metricsourcehost01/uimapi/TBD/CF1D7A708DD4C6C9D303025AE3D2334AE",
      "name": "CPU-1",
      "qosName": "QOS_CPU_MULTI_USAGE",
      "description": "Individual CPU Usage",
      "unit": "%"
    },
    "uimMetricDefinition": null,
    "minSampleValue": 59.85,
    "maxSampleValue": 72.31,
    "meanSampleValue": 64.296,
    "sample": [
      {
        "time": "2021-09-02T00:50:32.000Z",
        "timeSinceEpoch": 1630543832,
        "value": 59.85,
        "rate": 60
      },
      {
        "time": "2021-09-02T00:49:32.000Z",
        "timeSinceEpoch": 1630543772,
        "value": 63.88,
        "rate": 60
      },
      {
        "time": "2021-09-02T00:48:32.000Z",
        "timeSinceEpoch": 1630543712,
        "value": 60.17,
        "rate": 60
      },
      {
        "time": "2021-09-02T00:47:32.000Z",
        "timeSinceEpoch": 1630543652,
        "value": 65.27,
        "rate": 60
      },
      {
        "time": "2021-09-02T00:46:32.000Z",
        "timeSinceEpoch": 1630543592,
        "value": 72.31,
        "rate": 60
      }
    ]
  }
]

这是来自同一主机的 2 CPU 个内核的 CPU 利用率。

使用 $.[*].sample[*].time$.[*].sample[*].value 成功 returns 所需的时间和价值数据可以很容易地绘制成图表:

| time                     | value |
| ------------------------ | ----- |
| 2021-09-02T00:50:32.000Z | 61.17 |
| 2021-09-02T00:49:32.000Z | 63.52 |
| 2021-09-02T00:48:32.000Z | 62.79 |
| 2021-09-02T00:47:32.000Z | 64.06 |
| 2021-09-02T00:46:32.000Z | 72.78 |
| 2021-09-02T00:50:32.000Z | 59.85 |
| 2021-09-02T00:49:32.000Z | 63.88 |
| 2021-09-02T00:48:32.000Z | 60.17 |
| 2021-09-02T00:47:32.000Z | 65.27 |
| 2021-09-02T00:46:32.000Z | 72.31 |

但是,它结合了所有数据,无法区分两个 CPU 核心数据样本。

我一直在尝试找出一种方法,利用样本的每次迭代的目标获得第三列数组。

理想情况下,输出应该如下所示:

| target                                 | time                     | value |
| -------------------------------------- | ------------------------ | ----- |
| destinationhost01.our.domain.net-**0** | 2021-09-02T00:50:32.000Z | 61.17 |
| destinationhost01.our.domain.net-**0** | 2021-09-02T00:49:32.000Z | 63.52 |
| destinationhost01.our.domain.net-**0** | 2021-09-02T00:48:32.000Z | 62.79 |
| destinationhost01.our.domain.net-**0** | 2021-09-02T00:47:32.000Z | 64.06 |
| destinationhost01.our.domain.net-**0** | 2021-09-02T00:46:32.000Z | 72.78 |
| destinationhost01.our.domain.net-**1** | 2021-09-02T00:50:32.000Z | 59.85 |
| destinationhost01.our.domain.net-**1** | 2021-09-02T00:49:32.000Z | 63.88 |
| destinationhost01.our.domain.net-**1** | 2021-09-02T00:48:32.000Z | 60.17 |
| destinationhost01.our.domain.net-**1** | 2021-09-02T00:47:32.000Z | 65.27 |
| destinationhost01.our.domain.net-**1** | 2021-09-02T00:46:32.000Z | 72.31 |

如有任何建议,我们将不胜感激。我不确定 jsonpath 是否可行...因此我要联系专家。

谢谢

作为JSON API for Grafana uses the JSONPath Plus package,很容易实现我想要的。

^ 能够抓取任何匹配项的父项。在 JSONPath Demo 网站上玩这个让我到达了那里。您可以从原始 post 粘贴到我的示例中并测试以下查询:

  • $.[*].sample[*].time从每个样本中获取时间。
  • $.[*].sample[*].value从每个样本中获取值。
  • $.[*].sample[*].value^^^^.for_configuration_item.name是特制酱汁,每个样品for_configuration_item.name

向 Grafana 提供这三个查询会生成如下 table:

core time value
CPU-0 2021-09-02T00:50:32.000Z 61.17
CPU-0 2021-09-02T00:49:32.000Z 63.52
CPU-0 2021-09-02T00:48:32.000Z 62.79
CPU-0 2021-09-02T00:47:32.000Z 64.06
CPU-0 2021-09-02T00:46:32.000Z 72.78
CPU-1 2021-09-02T00:50:32.000Z 59.85
CPU-1 2021-09-02T00:49:32.000Z 63.88
CPU-1 2021-09-02T00:48:32.000Z 60.17
CPU-1 2021-09-02T00:47:32.000Z 65.27
CPU-1 2021-09-02T00:46:32.000Z 72.31

从那里开始,使用 Experimental 选项卡中 core 列中的 Group by 功能可以精确绘制值必填。