jsonq: github.com/jmoiron/jsonq Index | Files

package jsonq

import "github.com/jmoiron/jsonq"

Package jsonq simplify your json usage with a simple hierarchical query.

Given some json data like:

{
	"foo": 1,
	"bar": 2,
	"test": "Hello, world!",
	"baz": 123.1,
	"array": [
		{"foo": 1},
		{"bar": 2},
		{"baz": 3}
	],
	"subobj": {
		"foo": 1,
		"subarray": [1,2,3],
		"subsubobj": {
			"bar": 2,
			"baz": 3,
			"array": ["hello", "world"]
		}
	},
	"bool": true
}

Decode it into a map[string]interrface{}:

import (
	"strings"
	"encoding/json"
	"github.com/jmoiron/jsonq"
)

data := map[string]interface{}{}
dec := json.NewDecoder(strings.NewReader(jsonstring))
dec.Decode(&data)
jq := jsonq.NewQuery(data)

From here, you can query along different keys and indexes:

// data["foo"] -> 1
jq.Int("foo")

// data["subobj"]["subarray"][1] -> 2
jq.Int("subobj", "subarray", "1")

// data["subobj"]["subarray"]["array"][0] -> "hello"
jq.String("subobj", "subsubobj", "array", "0")

// data["subobj"] -> map[string]interface{}{"subobj": ...}
obj, err := jq.Object("subobj")

Notes:

Missing keys, out of bounds indexes, and type failures will return errors. For simplicity, integer keys (ie, {"0": "zero"}) are inaccessible by `jsonq` as integer strings are assumed to be array indexes.

Index

Package Files

doc.go jsonq.go

type JsonQuery Uses

type JsonQuery struct {
    // contains filtered or unexported fields
}

JsonQuery is an object that enables querying of a Go map with a simple positional query language.

func NewQuery Uses

func NewQuery(data interface{}) *JsonQuery

NewQuery creates a new JsonQuery obj from an interface{}.

func (*JsonQuery) Array Uses

func (j *JsonQuery) Array(s ...string) ([]interface{}, error)

Array extracts a []interface{} from the JsonQuery

func (*JsonQuery) ArrayOfArrays Uses

func (j *JsonQuery) ArrayOfArrays(s ...string) ([][]interface{}, error)

ArrayOfArrays extracts an array of []interface{} (arrays) from some json

func (*JsonQuery) ArrayOfBools Uses

func (j *JsonQuery) ArrayOfBools(s ...string) ([]bool, error)

ArrayOfBools extracts an array of bools from some json

func (*JsonQuery) ArrayOfFloats Uses

func (j *JsonQuery) ArrayOfFloats(s ...string) ([]float64, error)

ArrayOfFloats extracts an array of float64s from some json

func (*JsonQuery) ArrayOfInts Uses

func (j *JsonQuery) ArrayOfInts(s ...string) ([]int, error)

ArrayOfInts extracts an array of ints from some json

func (*JsonQuery) ArrayOfObjects Uses

func (j *JsonQuery) ArrayOfObjects(s ...string) ([]map[string]interface{}, error)

ArrayOfObjects extracts an array of map[string]interface{} (objects) from some json

func (*JsonQuery) ArrayOfStrings Uses

func (j *JsonQuery) ArrayOfStrings(s ...string) ([]string, error)

ArrayOfStrings extracts an array of strings from some json

func (*JsonQuery) Bool Uses

func (j *JsonQuery) Bool(s ...string) (bool, error)

Bool extracts a bool the JsonQuery

func (*JsonQuery) Float Uses

func (j *JsonQuery) Float(s ...string) (float64, error)

Float extracts a float from the JsonQuery

func (*JsonQuery) Int Uses

func (j *JsonQuery) Int(s ...string) (int, error)

Int extracts an int from the JsonQuery

func (*JsonQuery) Interface Uses

func (j *JsonQuery) Interface(s ...string) (interface{}, error)

Interface extracts an interface{} from the JsonQuery

func (*JsonQuery) Matrix2D Uses

func (j *JsonQuery) Matrix2D(s ...string) ([][]interface{}, error)

Matrix2D is an alias for ArrayOfArrays

func (*JsonQuery) Object Uses

func (j *JsonQuery) Object(s ...string) (map[string]interface{}, error)

Object extracts a json object from the JsonQuery

func (*JsonQuery) String Uses

func (j *JsonQuery) String(s ...string) (string, error)

String extracts a string from the JsonQuery

Package jsonq imports 2 packages (graph) and is imported by 40 packages. Updated 2017-08-18. Refresh now. Tools for package owners.