2

I had a Json of format

{
    ...,
    "tclist":[{
        "tcID":"TC1",
        "tcp":"/home/1.py",
        "time":"20:00:40"
    }, {
        "tcID":"TC2",
        "tcp":"/home/1.py",
        "time":"048:50:06"
    }],
    ...
}

I want to create a Map that takes a tcp as key and add the tcID and time to it as a entry in a set.

I Want

[["/home/1.py"][{tcID,Time},{tcID,Time}],[["/home/2.py"][{tcID,Time},{tcID,Time}]]
mbuechmann
  • 5,413
  • 5
  • 27
  • 40
user8800020
  • 133
  • 1
  • 3
  • 6

1 Answers1

3

You can define a custom type, backed by a map, and then define a custom unmarshaller on that type.

Here is a runnable example in the go playground

// the value in the map that you are unmarshalling to
type TCPValue struct {
    TcID string
    Time string
}

// the map type you are unmarshalling to
type TCPSet map[string][]TCPValue

// custom unmarshalling method that implements json.Unmarshaller interface
func (t *TCPSet) UnmarshalJSON(b []byte) error {
    // Create a local struct that mirrors the data being unmarshalled
    type tcEntry struct {
        TcID string `json:"tcID"`
        TCP string `json:"tcp"`
        Time string `json:"time"`
    }

    var entries []tcEntry

    // unmarshal the data into the slice
    if err := json.Unmarshal(b, &entries); err != nil {
        return err
    }

    tmp := make(TCPSet)

    // loop over the slice and create the map of entries
    for _, ent := range entries {
        tmp[ent.TCP] = append(tmp[ent.TCP], TCPValue{TcID: ent.TcID, Time: ent.Time})
    }

    // assign the tmp map to the type
    *t = tmp
    return nil
} 

You'll be able to access the elements like a regular map:

elem := tcpSet["/home/1.py"]

Edited based on comment from OP to be map[string][]TCPValue

Zak
  • 5,515
  • 21
  • 33
  • What you produced is map[/home/1.py:{TcID:TC3 Time:20:20:20} /home/2.py:{TcID:TC2 Time:048:50:06}] I wanted is map[/home/1.py:[{TcID:TC3 Time:20:20:20},{TcId:TC2 Time:20:20:12}] /home/2.py:[{TcID:TC2 Time:048:50:06}]] – user8800020 Jun 15 '18 at 05:34