Last active
October 10, 2017 14:37
-
-
Save arehmandev/818b0c54f4d12db296ab21c2475e5ac0 to your computer and use it in GitHub Desktop.
Hi buddy - gave it a go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "log" | |
| "time" | |
| ) | |
| // Fulldata - test | |
| type Fulldata struct { | |
| Fulldata []Dataset `json:"data"` | |
| } | |
| // Dataset - test | |
| type Dataset struct { | |
| IP string `json:"ip"` | |
| ID string `json:"id"` | |
| Time time.Time `json:"time"` | |
| Smethod string `json:"smethod"` | |
| Level string `json:"level"` | |
| Data string `json:"data"` | |
| } | |
| var testdata = `{ | |
| "data": [{ | |
| "ip": "192.168.2.1", | |
| "id": "1", | |
| "time": "0001-01-01T00:00:00Z", | |
| "smethod": "This is Method", | |
| "level": "Level 1", | |
| "data": "This is data" | |
| }, { | |
| "ip": "192.168.2.1", | |
| "id": "2", | |
| "time": "0001-01-01T00:00:00Z", | |
| "smethod": "This is Method2", | |
| "level": "Level 2", | |
| "data": "This is data2" | |
| }, { | |
| "ip": "192.168.2.2", | |
| "id": "3", | |
| "time": "0001-01-01T00:00:00Z", | |
| "smethod": "This is Method2", | |
| "level": "Level 3", | |
| "data": "This is data3" | |
| }] | |
| }` | |
| func main() { | |
| // Instantiate struct as variable | |
| mydata := Fulldata{} | |
| // Data converted to byte for unmarshalling | |
| bytedata := []byte(testdata) | |
| // Unmarshal data | |
| json.Unmarshal(bytedata, &mydata) | |
| // Do the magic | |
| filteredslice := filterjson(mydata.Fulldata, "192.168.2.1") | |
| // Complete struct and marshal back into JSON | |
| mydata.Fulldata = filteredslice | |
| K, err := json.MarshalIndent(mydata, "", " ") // Pretty printing (using MarshalIndent with 2 spaces, rather than just Marshal) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| finaljson := string(K) | |
| fmt.Println(finaljson) | |
| } | |
| func filterjson(datasetslice []Dataset, ipvalue string) (filteredset []Dataset) { | |
| // Loop through datasets for values | |
| for _, element := range datasetslice { | |
| // If the IP matches, append the dataset to a slice | |
| if element.IP == ipvalue { | |
| filteredset = append(filteredset, element) | |
| } | |
| } | |
| return filteredset | |
| } | |
| // If you wish to see something a little more complex, see this: https://gist.github.com/arehmandev/87a63bf5964ce4a8833294fb831c6373 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "log" | |
| "time" | |
| ) | |
| // Fulldata - test | |
| type Fulldata struct { | |
| Fulldata []Dataset `json:"data"` | |
| } | |
| // Dataset - test | |
| type Dataset struct { | |
| IP string `json:"ip"` | |
| ID string `json:"id"` | |
| Time time.Time `json:"time"` | |
| Smethod string `json:"smethod"` | |
| Level string `json:"level"` | |
| Data string `json:"data"` | |
| } | |
| var testdata1 = `{ | |
| "ip": "192.168.2.1", | |
| "id": "1", | |
| "time": "0001-01-01T00:00:00Z", | |
| "smethod": "This is Method", | |
| "level": "Level 1", | |
| "data": "This is data" | |
| }` | |
| var testdata2 = `{ | |
| "ip": "192.168.2.1", | |
| "id": "2", | |
| "time": "0001-01-01T00:00:00Z", | |
| "smethod": "This is Method2", | |
| "level": "Level 2", | |
| "data": "This is data2" | |
| }` | |
| var testdata3 = `{ | |
| "ip": "192.168.2.2", | |
| "id": "3", | |
| "time": "0001-01-01T00:00:00Z", | |
| "smethod": "This is Method2", | |
| "level": "Level 3", | |
| "data": "This is data3" | |
| }` | |
| func main() { | |
| // Instantiate struct as variable | |
| mydata := Fulldata{} | |
| mydataset1 := Dataset{} | |
| mydataset2 := Dataset{} | |
| mydataset3 := Dataset{} | |
| // Data converted to byte for unmarshalling | |
| bytedata1 := []byte(testdata1) | |
| bytedata2 := []byte(testdata2) | |
| bytedata3 := []byte(testdata3) | |
| // Unmarshal data into a Dataset | |
| json.Unmarshal([]byte(bytedata1), &mydataset1) | |
| json.Unmarshal([]byte(bytedata2), &mydataset2) | |
| json.Unmarshal([]byte(bytedata3), &mydataset3) | |
| // Create slice []Dataset | |
| unfilteredslice := []Dataset{mydataset1, mydataset2, mydataset3} | |
| // Do the magic | |
| filteredslice := filterjson(unfilteredslice, "192.168.2.1") | |
| // Complete struct and marshal back into JSON | |
| mydata.Fulldata = filteredslice | |
| K, err := json.MarshalIndent(mydata, "", " ") // Pretty printing (using MarshalIndent with 2 spaces, rather than just Marshal) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| finaljson := string(K) | |
| fmt.Println(finaljson) | |
| } | |
| func filterjson(datasetslice []Dataset, ipvalue string) (filteredset []Dataset) { | |
| // Loop through datasets for values | |
| for _, element := range datasetslice { | |
| // If the IP matches, append the dataset to a slice | |
| if element.IP == ipvalue { | |
| filteredset = append(filteredset, element) | |
| } | |
| } | |
| return filteredset | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment