简体   繁体   中英

Go Template - remove specific field

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}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM