Skip to content
/ jsone Public

Golang map[string]interface{} dive function for json decoding

License

Notifications You must be signed in to change notification settings

allochi/jsone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jsone

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.

Getting started

  1. Download
go get github.com/allochi/jsone
  1. Import
import "github.com/allochi/jsone"

Usage

Jsone currently has only one public function jsone.Dive().

Basically:

  1. use json.Unmarshal() to decode a json into either map[string]interface{} or []interface{}.
  2. 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")

About

Golang map[string]interface{} dive function for json decoding

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages