英文:
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
- 找到 
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
}
正如您所看到的,没有 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 
timefield incolumns - remove the corresponding data elements from 
values 
 - find the position of 
 
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论