I have this code:
package main
import (
"encoding/json"
"fmt"
)
// JSONMap is a specialization from map
type JSONMap = map[string]interface{}
// JSONAggregation is a specialization from map[]
type JSONAggregation = map[string][]interface{}
// JSONtoMap convert the json string to a JSONMap compatible with Go
func JSONtoMap(originalJSON string) JSONMap {
var result map[string]interface{}
json.Unmarshal([]byte(originalJSON), &result)
return result
}
func aggregateInterfaces(aggregation *JSONAggregation, target JSONMap) {
ref := *aggregation
// iterate through every target (JSONMap) and append it to the aggregation
// if its the first time the key is seen, then allocate memory
// and then append it
/*layout := "2006-01-02T15:04:05.000Z"
t, err := time.Parse(layout, fmt.Sprint(target["date"]))
if err != nil {
fmt.Println(err)
}*/
if v, found := target["date"]; found {
fmt.Println(v)
str := fmt.Sprint(v)
fmt.Println(str[11:13])
}
for key := range target {
if _, ok := ref[key]; ok {
ref[key] = append(ref[key], target[key])
} else {
ref[key] = make([]interface{}, 0)
ref[key] = append(ref[key], target[key])
}
}
}
// aggregateJSONS return a JSONAggregation with all jsons aggregated
// given that is provided an array of jsons on string format
func aggregateJSONS(jsons []string) JSONAggregation {
aggregation := make(JSONAggregation, 0)
for _, result := range jsons {
aggregateInterfaces(&aggregation, JSONtoMap(result))
}
return aggregation
}
func main() {
jsons := []string{
`{
"name":"kate",
"date":"2012-04-23T18:24:59.511Z",
"data":"is nice"
}`,
`{
"name":"gleison",
"date":"2012-04-23T18:25:00.511Z",
"data":"is a good person"
}`,
`{
"name":"breno",
"date":"2012-04-23T19:25:01.511Z",
"data":"is a cool person"
}`,
}
fmt.Println(aggregateJSONS(jsons))
}
And it the end i got
2012-04-23T18:24:59.511Z
18
2012-04-23T18:25:00.511Z
18
2012-04-23T19:25:01.511Z
19
map[data:[is nice is a good person is a cool person] date:[2012-04-23T18:24:59.511Z 2012-04-23T18:25:00.511Z 2012-04-23T19:25:01.511Z] name:[kate gleison breno]]
My question is: how can i take an access to the field “date”. What i need to do: aggregate them to groups by hours, days, weeks and months. Categorize them and then aggregate. How can i compare date’s by each other?