Golang JSON processing without structure constraints
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



-- import "chillson"

Chillson package is a convenience tool, providing way to process unmarshaled JSON data in schema-agnostic way. Thus you don't have to specify type structure conforming to the expected JSON structure, or write countless Golang type assertions. The latter is automated by chillson's Son type methods, like in the example below.

    import (

    var jsonData interface{}
    json.Unmarshal([]byte(rawJson), &jsonData)
    chill := chillson.Son{jsonData}

    // now you can use Son-type variable like this:
    strField, err := chill.GetStr("[gophers][0][name]")
    intField, err := chill.GetInt("[gophers][0][weight]")

    // you can also spawn "smaller" Son{}'s to avoid some underlying type assertions:
    gophersTable, err := chill.GetArr("[gophers]")
    for i := 0; i < len(gophersTable); i++ {
	    gophersRow := chillson.Son{gophersTable[i]}
	    strField, err = chill.GetStr("[name]")
	    intField, err = chill.GetInt("[weight]")

    // testing for specific error values, if you happen to need them:
    switch err {
    case nil:
    case InvalidPath:     // "Chillson: cannot parse nonempty value path, did you forget about square brackets?"
    case OutOfRange:      // "Chillson: value's parent seems to be a JSON array, but the index is out of range."
    case ParentNotObject: // "Chillson: value's parent is neither JSON object nor array."
    case FieldNotFound:    // "Chillson: value's parent seems to be a JSON object, but the field cannot be found."
    case BadValueType:    // "Chillson: retrieved value cannot be converted to the requested type."

Chillson is MIT-licensed (see LICENSE).


const (
	InvalidPath chillsonErr = iota

type Son

type Son struct {
	Data (interface{})

Son wraps an unmarshaled JSON document.

func (*Son) Get

func (c *Son) Get(path string) (interface{}, error)

Get() returns value from given location in Son data. Object keys and array indices should be both enclosed in [square brackets], WITHOUT "quotation marks". String indices (= object keys) can be arbitrary JSON strings as in JSON source, but they shouldn't contain square brackets [ ].

func (*Son) GetArr

func (c *Son) GetArr(path string) ([]interface{}, error)
GetArr returns a JSON array as Golang slice.

func (*Son) GetFloat

func (c *Son) GetFloat(path string) (float64, error)

GetFloat returns JSON number as Golang float64.

func (*Son) GetInt

func (c *Son) GetInt(path string) (int, error)

func (*Son) GetObj

func (c *Son) GetObj(path string) (map[string]interface{}, error)

func (*Son) GetStr

func (c *Son) GetStr(path string) (string, error)