Jsone.Dive()
is a function that can be used to dive into map[string]interface{}
that represent a json structure and read a node.
It's simple to use and straight forward. Not yet fully tested, still WIP but it worked so far.
- Download
go get github.com/allochi/jsone
- Import
import "github.com/allochi/jsone"
Jsone currently has only one public function jsone.Dive()
.
Basically:
- use
json.Unmarshal()
to decode a json into eithermap[string]interface{}
or[]interface{}
. - use a
string
or a[]interface{}
as the path of the node to retrieve.
var content []byte
var root map[string]interface{}
var err error
content, err = ioutil.ReadFile("sample.json")
err = json.Unmarshal(content, &root)
skills, err := jsone.Dive(root, []interface{}{"skills"})
// or
skills, err := jsone.Dive(root, "skills")
"personal/favorites/color"
color, err := jsone.Dive(root, []interface{}{"personal", "favorites", "color"})
// or
color, err := jsone.Dive(root, "personal/favorites/color")
interest, err := jsone.Dive(root, []interface{}{"personal", "interests", 1})
// or
interest, err := jsone.Dive(root, "personal/interests/1")
name, err := jsone.Dive(skills, "tests/0/name")
name, err := jsone.Dive(skills, "tests/1/name")