Skip to content

Instantly share code, notes, and snippets.

@danielvaughan
Created October 24, 2018 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielvaughan/fb32abd2101935a02e1513c54519a0f2 to your computer and use it in GitHub Desktop.
Save danielvaughan/fb32abd2101935a02e1513c54519a0f2 to your computer and use it in GitHub Desktop.
package main
import (
"compress/gzip"
"encoding/json"
"fmt"
"log"
"os"
)
func main() {
//This struct acts as a model for the JSON you are reading
type Sample struct {
Name string `json:"name"`
Accession string `json:"accession"`
}
inputFile := os.Args[1]
f, err := os.Open(inputFile)
if err != nil {
log.Fatal(err)
}
gz, err := gzip.NewReader(f)
if err != nil {
log.Fatal(err)
}
defer f.Close()
defer gz.Close()
dec := json.NewDecoder(gz)
// read open bracket
_, err = dec.Token()
if err != nil {
log.Fatal(err)
}
// while the array contains values
for dec.More() {
var s Sample
// decode an array value (Sample)
err := dec.Decode(&s)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v:%v\n", s.Name, s.Accession)
}
// read closing bracket
_, err = dec.Token()
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment