使用 JQ 按名称更改 JSON 中的值

Changing a value in JSON by name using JQ

我正在使用 JQ 调整 Grafana 仪表板。我这样做是为了可以将特定于环境的值更改为仪表板 json,然后采用修改后的 json 并将其部署到新环境中。

我想更改的一个值是存储在表示 Grafana 变量的对象中的 query 字段。此对象的名称 属性 为“Environment”。我只关心这个对象,其他所有变量定义对象(附例只包含一个)将保持不变。

我试过的 因为我需要搜索具有特定名称 属性 的对象,所以我使用 select。我正在使用 select,因为我不相信有任何其他方式可以到达特定元素。

我试过这个查询

select(.templating.list[].name=="Environment").query="dev"

我正在使用“环境”的“名称”属性寻找变量定义。然后我想将其值更改为“dev”。

期望输出

我希望整个 JSON 负载返回新的查询值:dev

结果 我没有收到任何错误,但该值也没有更改。

我不知道该怎么做。你能帮忙吗?

Json 如下:

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": null,
  "graphTooltip": 0,
  "id": 5,
  "iteration": 1622058997292,
  "links": [],
  "panels": [
    {
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": null,
      "fieldConfig": {
        "defaults": {},
        "overrides": []
      },
      "fill": 1,
      "fillGradient": 0,
      "gridPos": {
        "h": 9,
        "w": 12,
        "x": 0,
        "y": 0
      },
      "hiddenSeries": false,
      "id": 2,
      "legend": {
        "avg": false,
        "current": false,
        "max": false,
        "min": false,
        "show": true,
        "total": false,
        "values": false
      },
      "lines": true,
      "linewidth": 1,
      "nullPointMode": "null",
      "options": {
        "alertThreshold": true
      },
      "percentage": false,
      "pluginVersion": "7.5.4",
      "pointradius": 2,
      "points": false,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "alias": "",
          "dimensions": {},
          "expression": "",
          "id": "",
          "matchExact": true,
          "metricName": "",
          "namespace": "",
          "period": "",
          "refId": "A",
          "region": "default",
          "statistics": [
            "Average"
          ]
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "Panel Title",
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        },
        {
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    }
  ],
  "schemaVersion": 27,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": [
      {
        "description": null,
        "error": null,
        "hide": 2,
        "label": null,
        "name": "Environment",
        "query": "prod",
        "skipUrlSync": false,
        "type": "constant"
      }
    ]
  },
  "time": {
    "from": "now-6h",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "test",
  "uid": "Op2N9sqGk",
  "version": 1
}

你快到了:

(.templating.list[] | select(.name == "Environment" ) | .query) |= "dev"

需要考虑的其他一些可能性:

不专心

walk (if type == "object" and has("query") then .query = "dev" else . end)

或没有walk:

(.. | objects | select(has("query")).query) |= "dev"

更专注

walk (if type == "object" and .name == "Environment" and has("query") 
      then .query = "dev" else . end)

或没有walk:

(.. | objects | select(.name == "Environment" and has("query"))).query |= "dev"