Documentation ¶
Overview ¶
Package gojsonq provides a simple, elegant and fast ODM like API to access/query JSON document.
JSON document can be read from file, string or io.Reader. Accessing the value of json property or querying document is simple as the example below:
package main import "github.com/thedevsaddam/gojsonq" const json = `{"name":{"first":"Tom","last":"Hanks"},"age":61}` func main() { name := gojsonq.New().FromString(json).Find("name.first") println(name.(string)) // Tom }
For more details, see the documentation and examples.
Index ¶
- Constants
- Variables
- type Decoder
- type DefaultDecoder
- type JSONQ
- func (j *JSONQ) Avg(property ...string) float64
- func (j *JSONQ) Copy() *JSONQ
- func (j *JSONQ) Count() int
- func (j *JSONQ) Distinct(property string) *JSONQ
- func (j *JSONQ) Error() error
- func (j *JSONQ) Errors() []error
- func (j *JSONQ) File(filename string) *JSONQ
- func (j *JSONQ) Find(path string) interface{}
- func (j *JSONQ) FindR(path string) (*Result, error)
- func (j *JSONQ) First() interface{}
- func (j *JSONQ) FirstR() (*Result, error)
- func (j *JSONQ) From(node string) *JSONQ
- func (j *JSONQ) FromInterface(v interface{}) *JSONQ
- func (j *JSONQ) FromString(str string) *JSONQ
- func (j *JSONQ) Get() interface{}
- func (j *JSONQ) GetR() (*Result, error)
- func (j *JSONQ) GroupBy(property string) *JSONQ
- func (j *JSONQ) JSONString(json string) *JSONQ
- func (j *JSONQ) Last() interface{}
- func (j *JSONQ) LastR() (*Result, error)
- func (j *JSONQ) Limit(limit int) *JSONQ
- func (j *JSONQ) Macro(operator string, fn QueryFunc) *JSONQ
- func (j *JSONQ) Max(property ...string) float64
- func (j *JSONQ) Min(property ...string) float64
- func (j *JSONQ) More() *JSONQ
- func (j *JSONQ) Nth(index int) interface{}
- func (j *JSONQ) NthR(index int) (*Result, error)
- func (j *JSONQ) Offset(offset int) *JSONQ
- func (j *JSONQ) Only(properties ...string) interface{}
- func (j *JSONQ) OnlyR(properties ...string) (*Result, error)
- func (j *JSONQ) OrWhere(key, cond string, val interface{}) *JSONQ
- func (j *JSONQ) Out(v interface{})
- func (j *JSONQ) Pluck(property string) interface{}
- func (j *JSONQ) PluckR(property string) (*Result, error)
- func (j *JSONQ) Reader(r io.Reader) *JSONQ
- func (j *JSONQ) Reset() *JSONQ
- func (j *JSONQ) Select(properties ...string) *JSONQ
- func (j *JSONQ) Sort(order ...string) *JSONQ
- func (j *JSONQ) SortBy(order ...string) *JSONQ
- func (j *JSONQ) String() string
- func (j *JSONQ) Sum(property ...string) float64
- func (j *JSONQ) Where(key, cond string, val interface{}) *JSONQ
- func (j *JSONQ) WhereContains(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereEndsWith(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereEqual(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereIn(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereLenEqual(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereLenNotEqual(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereNil(key string) *JSONQ
- func (j *JSONQ) WhereNotEqual(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereNotIn(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereNotNil(key string) *JSONQ
- func (j *JSONQ) WhereStartsWith(key string, val interface{}) *JSONQ
- func (j *JSONQ) WhereStrictContains(key string, val interface{}) *JSONQ
- func (j *JSONQ) Writer(w io.Writer)
- type OptionFunc
- type Query
- type QueryFunc
- type Result
- func (r *Result) As(v interface{}) error
- func (r *Result) Bool() (bool, error)
- func (r *Result) BoolSlice() ([]bool, error)
- func (r *Result) Duration() (time.Duration, error)
- func (r *Result) DurationSlice() ([]time.Duration, error)
- func (r *Result) Float32() (float32, error)
- func (r *Result) Float32Slice() ([]float32, error)
- func (r *Result) Float64() (float64, error)
- func (r *Result) Float64Slice() ([]float64, error)
- func (r *Result) Int() (int, error)
- func (r *Result) Int16() (int16, error)
- func (r *Result) Int16Slice() ([]int16, error)
- func (r *Result) Int32() (int32, error)
- func (r *Result) Int32Slice() ([]int32, error)
- func (r *Result) Int64() (int64, error)
- func (r *Result) Int64Slice() ([]int64, error)
- func (r *Result) Int8() (int8, error)
- func (r *Result) Int8Slice() ([]int8, error)
- func (r *Result) IntSlice() ([]int, error)
- func (r *Result) Nil() bool
- func (r *Result) String() (string, error)
- func (r *Result) StringSlice() ([]string, error)
- func (r *Result) Time(layout string) (time.Time, error)
- func (r *Result) TimeSlice(layout string) ([]time.Time, error)
- func (r *Result) Uint() (uint, error)
- func (r *Result) Uint16() (uint16, error)
- func (r *Result) Uint16Slice() ([]uint16, error)
- func (r *Result) Uint32() (uint32, error)
- func (r *Result) Uint32Slice() ([]uint32, error)
- func (r *Result) Uint64() (uint64, error)
- func (r *Result) Uint64Slice() ([]uint64, error)
- func (r *Result) Uint8() (uint8, error)
- func (r *Result) Uint8Slice() ([]uint8, error)
- func (r *Result) UintSlice() ([]uint, error)
Constants ¶
const ( DateTimeFormat = "2006-01-02T15:04:05" DateTimeSpaceFormat = "2006-01-02 15:04:05" DateFormat = "2006-01-02" Select = "s:" Where = "w:" From = "f:" Limit = "l:" Offset = "p:" SortBy = "o:" Order = "d:" Distinct = "u:" GroupBy = "g:" Time = "t:" Each = "e:" PipeSum = "sum" PipeCount = "count" PipeMax = "max" PipeMin = "min" PipeAvg = "avg" PipeFirst = "first" PipeLast = "last" PipeNth = "nth" PipeFind = "find" PipePluck = "pluck" PipeOnly = "only" )
Variables ¶
var ( ErrExpectsPointer = fmt.Errorf("gojsonq: failed to unmarshal, expects pointer") ErrImmutable = fmt.Errorf("gojsonq: failed to unmarshal, target is not mutable") ErrTypeMismatch = fmt.Errorf("gojsonq: failed to unmarshal, target type misatched") )
Available named error values
Functions ¶
This section is empty.
Types ¶
type DefaultDecoder ¶
type DefaultDecoder struct{}
DefaultDecoder use json.Unmarshal to decode JSON
func (*DefaultDecoder) Decode ¶
func (u *DefaultDecoder) Decode(data []byte, v interface{}) error
Decode decodes using json.Unmarshal
type JSONQ ¶
type JSONQ struct {
// contains filtered or unexported fields
}
JSONQ describes a JSONQ type which contains all the state
func (*JSONQ) Copy ¶
Copy returns a new fresh instance of JSONQ with the original copy of data so that you can do concurrent operation on the same data without being decoded again
func (*JSONQ) Count ¶
Count returns the number of total items. This could be a length of list/array/map
func (*JSONQ) From ¶
From seeks the json content to provided node. e.g: "users.[0]" or "users.[0].name"
func (*JSONQ) FromInterface ¶
FromInterface reads the content from valid map[string]interface{}
func (*JSONQ) FromString ¶
FromString reads the content from valid json/xml/csv/yml string
func (*JSONQ) GroupBy ¶
GroupBy builds a chunk of exact matched data in a group list using provided attribute/column/property
func (*JSONQ) JSONString ¶
JSONString reads the json content from valid json string Deprecated: this method will remove in next major release
func (*JSONQ) More ¶
More provides the functionality to query over the resultant data. See https://github.com/thedevsaddam/gojsonq/wiki/Queries#More
func (*JSONQ) OnlyR ¶
OnlyR collects the properties from a list of object and return as Result instance
func (*JSONQ) Out ¶
func (j *JSONQ) Out(v interface{})
Out write the queried data to defined custom type
func (*JSONQ) PluckR ¶
PluckR build an array of values form a property of a list of objects and return as Result instance
func (*JSONQ) Reset ¶
Reset resets the current state of JSON instance and make a fresh object with the original json content
func (*JSONQ) SortBy ¶
SortBy sorts an array default ascending order, pass "desc" for descending order
func (*JSONQ) WhereContains ¶
WhereContains satisfies Where clause which contains provided value(string)
func (*JSONQ) WhereEndsWith ¶
WhereEndsWith satisfies Where clause which ends with provided value(string)
func (*JSONQ) WhereEqual ¶
WhereEqual is an alias of Where("key", "=", val)
func (*JSONQ) WhereLenEqual ¶
WhereLenEqual is an alias of Where("key", "leneq", val)
func (*JSONQ) WhereLenNotEqual ¶
WhereLenNotEqual is an alias of Where("key", "lenneq", val)
func (*JSONQ) WhereNotEqual ¶
WhereNotEqual is an alias of Where("key", "!=", val)
func (*JSONQ) WhereNotIn ¶
WhereNotIn is an alias for where("key", "notIn", []string{"a", "b"})
func (*JSONQ) WhereNotNil ¶
WhereNotNil is an alias of Where("key", "!=", nil)
func (*JSONQ) WhereStartsWith ¶
WhereStartsWith satisfies Where clause which starts with provided value(string)
func (*JSONQ) WhereStrictContains ¶
WhereStrictContains satisfies Where clause which contains provided value(string). This is case sensitive
type OptionFunc ¶
OptionFunc represents a contract for option func, it basically set options to jsonq instance options
func SetDecoder ¶
func SetDecoder(u Decoder) OptionFunc
SetDecoder take a custom decoder to decode JSON Deprecated - use WithDecoder
func SetSeparator ¶
func SetSeparator(s string) OptionFunc
SetSeparator set custom separator for traversing child node, default separator is DOT (.) Deprecated - use WithSeparator
func WithDecoder ¶
func WithDecoder(u Decoder) OptionFunc
WithDecoder take a custom decoder to decode JSON
func WithSeparator ¶
func WithSeparator(s string) OptionFunc
WithSeparator set custom separator for traversing child node, default separator is DOT (.)
type Query ¶
type Query struct { Raw string Select []string SelectMapAsArray bool Each string From string Where [][]query Limit int Offset int SortBy string Order string Distinct string Pipe string PipeParam string GroupBy string Next *Query }
func (*Query) PipeResult ¶
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result represent custom type
func (*Result) As ¶
As sets the value of Result to v; It does not support methods with argument available in Result
func (*Result) DurationSlice ¶
DurationSlice assert the result to []time.Duration
func (*Result) Float32Slice ¶
Float32Slice assert the result to []float32
func (*Result) Float64Slice ¶
Float64Slice assert the result to []float64
func (*Result) Int16Slice ¶
Int16Slice assert the result to []int16
func (*Result) Int32Slice ¶
Int32Slice assert the result to []int32
func (*Result) Int64Slice ¶
Int64Slice assert the result to []int64
func (*Result) StringSlice ¶
StringSlice assert the result to []string
func (*Result) Uint16Slice ¶
Uint16Slice assert the result to []uint16
func (*Result) Uint32Slice ¶
Uint32Slice assert the result to []uint32
func (*Result) Uint64Slice ¶
Uint64Slice assert the result to []uint64
func (*Result) Uint8Slice ¶
Uint8Slice assert the result to []uint8