【问题标题】:Changing a value in JSON by name using JQ使用 JQ 按名称更改 JSON 中的值
【发布时间】:2021-05-26 20:16:19
【问题描述】:

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

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

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

我试过这个查询

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

我正在寻找具有“环境”的“name”属性的变量定义。然后我想将其值更改为“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
}

【问题讨论】:

    标签: json jq grafana edit


    【解决方案1】:

    你快到了:

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

    【讨论】:

      【解决方案2】:

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

      注意力不集中

      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"
      

      【讨论】:

      • 这些是有趣的命令。它们在我的情况下不起作用,因为它们最终会更改所有“查询”键值,而不仅仅是用于环境的键值。他们不会吗?
      • “更专注”选项似乎满足您的要求。
      • 你是对的@peak,walk 只需稍作改动即可工作:walk (if type == "object" and .name == "Environment" and has("query") then . query = "dev" else .end) - 我不知道你可以将 walk 与 if 语句混合使用。
      猜你喜欢
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2017-07-31
      相关资源
      最近更新 更多