gojsonq package - github.com/thedevsaddam/gojsonq/v2 - Go Packages (original) (raw)

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.

This section is empty.

View Source

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

This section is empty.

type Decoder interface { Decode(data []byte, v interface{}) error }

Decoder provide contract to decode JSON using custom decoder

type DefaultDecoder struct{}

DefaultDecoder use json.Unmarshal to decode JSON

Decode decodes using json.Unmarshal

JSONQ describes a JSONQ type which contains all the state

func New(options ...OptionFunc) *JSONQ

New returns a new instance of JSONQ

Avg returns average of values from array or from map using property

func (j *JSONQ) Copy() *JSONQ

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 (j *JSONQ) Count() int

Count returns the number of total items. This could be a length of list/array/map

func (j *JSONQ) Distinct(property string) *JSONQ

Distinct builds distinct value using provided attribute/column/property

Error returns first occurred error

Errors returns list of all errors

File read the json content from physical file

func (j *JSONQ) Find(path string) interface{}

Find returns the result of a exact matching path

FindR returns the result as Result instance from the exact matching path

func (j *JSONQ) First() interface{}

First returns the first element of a list

FirstR returns the first element of a list as Result instance

From seeks the json content to provided node. e.g: "users.[0]" or "users.[0].name"

func (j *JSONQ) FromInterface(v interface{}) *JSONQ

FromInterface reads the content from valid map[string]interface{}

func (j *JSONQ) FromString(str string) *JSONQ

FromString reads the content from valid json/xml/csv/yml string

func (j *JSONQ) Get() interface{}

Get return the result

GetR return the query results as Result instance

func (j *JSONQ) GroupBy(property string) *JSONQ

GroupBy builds a chunk of exact matched data in a group list using provided attribute/column/property

func (j *JSONQ) JSONString(json string) *JSONQ

JSONString reads the json content from valid json string Deprecated: this method will remove in next major release

func (j *JSONQ) Last() interface{}

Last returns the last element of a list

LastR returns the last element of a list as Result instance

func (j *JSONQ) Limit(limit int) *JSONQ

Limit limits the number of records in result

func (j *JSONQ) Macro(operator string, fn QueryFunc) *JSONQ

Macro adds a new query func to the JSONQ

Max returns maximum value from array or from map using property

Min returns minimum value from array or from map using property

func (j *JSONQ) Nth(index int) interface{}

Nth returns the nth element of a list

NthR returns the nth element of a list as Result instance

func (j *JSONQ) Offset(offset int) *JSONQ

Offset skips the number of records in result

func (j *JSONQ) Only(properties ...string) interface{}

Only collects the properties from a list of object

OnlyR collects the properties from a list of object and return as Result instance

func (j *JSONQ) OrWhere(key, cond string, val interface{}) *JSONQ

OrWhere builds an OrWhere clause, basically it's a group of AND clauses

func (j *JSONQ) Out(v interface{})

Out write the queried data to defined custom type

func (j *JSONQ) Pluck(property string) interface{}

Pluck build an array of values form a property of a list of objects

PluckR build an array of values form a property of a list of objects and return as Result instance

Reader reads the json content from io reader

func (j *JSONQ) Reset() *JSONQ

Reset resets the current state of JSON instance and make a fresh object with the original json content

func (j *JSONQ) Select(properties ...string) *JSONQ

Select use for selection of the properties from query result

Sort sorts an array default ascending order, pass "desc" for descending order

func (j *JSONQ) SortBy(order ...string) *JSONQ

SortBy sorts an array default ascending order, pass "desc" for descending order

String satisfies stringer interface

Sum returns sum of values from array or from map using property

func (j *JSONQ) Where(key, cond string, val interface{}) *JSONQ

Where builds a where clause. e.g: Where("name", "contains", "doe")

func (j *JSONQ) WhereContains(key string, val interface{}) *JSONQ

WhereContains satisfies Where clause which contains provided value(string)

func (j *JSONQ) WhereEndsWith(key string, val interface{}) *JSONQ

WhereEndsWith satisfies Where clause which ends with provided value(string)

func (j *JSONQ) WhereEqual(key string, val interface{}) *JSONQ

WhereEqual is an alias of Where("key", "=", val)

func (j *JSONQ) WhereIn(key string, val interface{}) *JSONQ

WhereIn is an alias for where("key", "in", []string{"a", "b"})

func (j *JSONQ) WhereLenEqual(key string, val interface{}) *JSONQ

WhereLenEqual is an alias of Where("key", "leneq", val)

func (j *JSONQ) WhereLenNotEqual(key string, val interface{}) *JSONQ

WhereLenNotEqual is an alias of Where("key", "lenneq", val)

WhereNil is an alias of Where("key", "=", nil)

func (j *JSONQ) WhereNotEqual(key string, val interface{}) *JSONQ

WhereNotEqual is an alias of Where("key", "!=", val)

func (j *JSONQ) WhereNotIn(key string, val interface{}) *JSONQ

WhereNotIn is an alias for where("key", "notIn", []string{"a", "b"})

func (j *JSONQ) WhereNotNil(key string) *JSONQ

WhereNotNil is an alias of Where("key", "!=", nil)

func (j *JSONQ) WhereStartsWith(key string, val interface{}) *JSONQ

WhereStartsWith satisfies Where clause which starts with provided value(string)

func (j *JSONQ) WhereStrictContains(key string, val interface{}) *JSONQ

WhereStrictContains satisfies Where clause which contains provided value(string). This is case sensitive

Writer write the queried data to a io.Writer

type OptionFunc func(*JSONQ) error

OptionFunc represents a contract for option func, it basically set options to jsonq instance options

func SetDecoder(u Decoder) OptionFunc

SetDecoder take a custom decoder to decode JSON Deprecated - use WithDecoder

SetSeparator set custom separator for traversing child node, default separator is DOT (.) Deprecated - use WithSeparator

func WithDecoder(u Decoder) OptionFunc

WithDecoder take a custom decoder to decode JSON

WithSeparator set custom separator for traversing child node, default separator is DOT (.)

type QueryFunc func(x, y interface{}) (bool, error)

QueryFunc describes a conditional function which perform comparison

Result represent custom type

func NewResult(v interface{}) *Result

NewResult return an instance of Result

func (r *Result) As(v interface{}) error

As sets the value of Result to v; It does not support methods with argument available in Result

Bool assert the result to boolean value

BoolSlice assert the result to []bool

Duration assert the result to time.Duration

DurationSlice assert the result to []time.Duration

Float32 assert the result to float32

Float32Slice assert the result to []float32

Float64 assert the result to 64

Float64Slice assert the result to []float64

Int assert the result to int

Int16 assert the result to int16

Int16Slice assert the result to []int16

Int32 assert the result to int32

Int32Slice assert the result to []int32

Int64 assert the result to int64

Int64Slice assert the result to []int64

Int8 assert the result to int8

Int8Slice assert the result to []int8

IntSlice assert the result to []int

Nil check the query has result or not

String assert the result to String

StringSlice assert the result to []string

Time assert the result to time.Time

TimeSlice assert the result to []time.Time

Uint assert the result to uint

Uint16 assert the result to uint16

Uint16Slice assert the result to []uint16

Uint32 assert the result to uint32

Uint32Slice assert the result to []uint32

Uint64 assert the result to uint64

Uint64Slice assert the result to []uint64

Uint8 assert the result to uint8

Uint8Slice assert the result to []uint8

UintSlice assert the result to []uint