Skip to content

Instantly share code, notes, and snippets.

@marcus-crane
Created May 6, 2018 01:35
Show Gist options
  • Save marcus-crane/cbb98752d060b23f9cc39dbf65d2fe9d to your computer and use it in GitHub Desktop.
Save marcus-crane/cbb98752d060b23f9cc39dbf65d2fe9d to your computer and use it in GitHub Desktop.
Parsing a package.json file into an iterable struct in Go. In this case, it iterates through dependency names
package main
import (
"encoding/json"
"fmt"
)
type PackageJson struct {
Name string `json:"name"`
Dependencies Dependency `json:"dependencies"`
DevDependencies Dependency `json:"devDependencies"`
}
type Dependencies struct {
Item Dependency
}
type Dependency map[string]interface{}
func main() {
var pkgParsed PackageJson
pkgJson := []byte(`{"name": "my-cool-package", "dependencies": {"coffee-script": "^4.0.0", "taekwando": "^0.1.0"}, "devDependencies": {"fresh": "^1.2.3"}}`)
err := json.Unmarshal(pkgJson, &pkgParsed)
if err != nil {
panic(err)
}
for key, _ := range pkgParsed.Dependencies {
fmt.Println(key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment