Go模板 – 删除特定字段

huangapple go评论82阅读模式
英文:

Go Template - remove specific field

问题

我正在进行以下数据格式的翻译:

{"time":"2022-08-24T06:00:00Z","duration":0,"level":"OK","data":{"series":[{"name":"gnb_kpi","tags":{"ID":"1017","_field":"Success_rate%","cluster_id":"ec17-1017","swversion":"6.0"},"columns":["time","_value"],"values":[["2022-08-24T06:00:00Z","100"]]}]},"previousLevel":"CRITICAL","recoverable":true}

我想要从columns数组中删除_time字段,类似地,从values数组中删除时间戳。我想要的输出如下:

{"time":"2022-08-24T06:00:00Z","duration":0,"level":"OK","data":{"series":[{"name":"gnb_kpi","tags":{"ID":"1017","_field":"Success_rate%","cluster_id":"ec17-1017","swVersion":"6.0"},"columns":["_value"],"values":[["100"]]}]},"previousLevel":"CRITICAL","recoverable":true}
英文:

I am having following data format:

{"time":"2022-08-24T06:00:00Z","duration":0,"level":"OK","data":{"series":[{"name":"gnb_kpi","tags":{"ID":"1017","_field":"Success_rate%","cluster_id":"ec17-1017","swversion":"6.0"},"columns":["time","_value"],"values":[["2022-08-24T06:00:00Z","100"]]}]},"previousLevel":"CRITICAL","recoverable":true}

I want to remove the _time field from the columns array and similarily the timestamp from values array. The output I want is like this:

{"time":"2022-08-24T06:00:00Z","duration":0,"level":"OK","data":{"series":[{"name":"gnb_kpi","tags":{"ID":"1017","_field":"Success_rate%","cluster_id":"ec17-1017","swVersion":"6.0"},"columns":["_value"],"values":[["100"]]}]},"previousLevel":"CRITICAL","recoverable":true}

答案1

得分: 1

我使用了在线服务JSON-to-Go来生成与您的输入相对应的数据结构。它生成了以下代码:

type AutoGenerated struct {
    Time          time.Time `json:"time"`
    Duration      int       `json:"duration"`
    Level         string    `json:"level"`
    Data          struct {
        Series []struct {
            Name    string `json:"name"`
            Tags    struct {
                ID        string `json:"ID"`
                Field     string `json:"_field"`
                ClusterID string `json:"cluster_id"`
                Swversion string `json:"swversion"`
            } `json:"tags"`
            Columns []string        `json:"columns"`
            Values  [][]interface{} `json:"values"`
        } `json:"series"`
    } `json:"data"`
    PreviousLevel string `json:"previousLevel"`
    Recoverable   bool   `json:"recoverable"`
}

算法很简单:

  • 将 JSON 解析为生成的结构体
  • 遍历 series
    • 找到 columnstime 字段的位置
    • values 中删除相应的数据元素

输出结果如下(格式化):

{
  "time": "2022-08-24T06:00:00Z",
  "duration": 0,
  "level": "OK",
  "data": {
    "series": [
      {
        "name": "gnb_kpi",
        "tags": {
          "ID": "1017",
          "_field": "Success_rate%",
          "cluster_id": "ec17-1017",
          "swversion": "6.0"
        },
        "columns": [
          "_value"
        ],
        "values": [
          [
            "100"
          ]
        ]
      }
    ]
  },
  "previousLevel": "CRITICAL",
  "recoverable": true
}

正如您所看到的,没有 time 字段。

英文:

I used the online service JSON-to-Go to generate a data structure that corresponds to your input. It produced

	
type AutoGenerated struct {
	Time     time.Time `json:"time"`
	Duration int       `json:"duration"`
	Level    string    `json:"level"`
	Data     struct {
		Series []struct {
			Name string `json:"name"`
			Tags struct {
				ID        string `json:"ID"`
				Field     string `json:"_field"`
				ClusterID string `json:"cluster_id"`
				Swversion string `json:"swversion"`
			} `json:"tags"`
			Columns []string        `json:"columns"`
			Values  [][]interface{} `json:"values"`
		} `json:"series"`
	} `json:"data"`
	PreviousLevel string `json:"previousLevel"`
	Recoverable   bool   `json:"recoverable"`
}

The algorithm is simple:

  • parse JSON into the generated structure
  • iterate over series
    • find the position of time field in columns
    • remove the corresponding data elements from values

https://go.dev/play/p/rjnvmdBXCE4

Output is (beautified)

{
  "time": "2022-08-24T06:00:00Z",
  "duration": 0,
  "level": "OK",
  "data": {
    "series": [
      {
        "name": "gnb_kpi",
        "tags": {
          "ID": "1017",
          "_field": "Success_rate%",
          "cluster_id": "ec17-1017",
          "swversion": "6.0"
        },
        "columns": [
          "_value"
        ],
        "values": [
          [
            "100"
          ]
        ]
      }
    ]
  },
  "previousLevel": "CRITICAL",
  "recoverable": true
}

As you see, no time

huangapple
  • 本文由 发表于 2022年8月24日 17:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/73470787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定