gojsonq

package module
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2018 License: MIT Imports: 10 Imported by: 0

README

gojsonq

Build Status Project status Go Report Card Coverage Status GoDoc License

A simple Go package to Query over JSON Data. It provides simple, elegant and fast ODM like API to access, query JSON document

Installation

Install the package using

$ go get github.com/thedevsaddam/gojsonq
// or
$ go get gopkg.in/thedevsaddam/gojsonq.v1
Usage

To use the package import it in your *.go code

import "github.com/thedevsaddam/gojsonq"
// or
import "gopkg.in/thedevsaddam/gojsonq.v1"

Let's see a quick example:

package main

import "github.com/thedevsaddam/gojsonq"

const json = `{"name":{"first":"Tom","last":"Hanks"},"age":61}`

func main() {
	name := gojsonq.New().JSONString(json).Find("name.first")
	println(name.(string)) // Tom
}

Another example:

package main

import (
	"fmt"

	"github.com/thedevsaddam/gojsonq"
)

const json = `{"city":"dhaka","type":"weekly","temperatures":[30,39.9,35.4,33.5,31.6,33.2,30.7]}`

func main() {
	avg := gojsonq.New().JSONString(json).From("temperatures").Avg()
	fmt.Println(avg) // 33.471428571428575
}

You can query your document using the various query methods such as Find, First, Nth, Pluck, Where, OrWhere, WhereIn, WhereStartsWith, WhereEndsWith, WhereContains, Sort, GroupBy, SortBy and so on. Also you can aggregate data after query using Avg, Count, Max, Min, Sum etc.

Find more query API in Wiki page

Bugs and Issues

If you encounter any bugs or issues, feel free to open an issue at github.

Also, you can shoot me an email to mailto:thedevsaddam@gmail.com for hugs or bugs.

Credit

Special thanks to Nahid Bin Azhar for the inspiration and guidance for the package. Thanks to Ahmed Shamim Hasan Shaon for his support from the very beginning.

Contributors

Contribution

If you are interested to make the package better please send pull requests or create an issue so that others can fix. Read the contribution guide here

License

The gojsonq is an open-source software licensed under the MIT License.

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().JSONString(json).Find("name.first")
	 println(name.(string)) // Tom
 }

For more details, see the documentation and examples.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder added in v1.6.0

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

Decoder provide contract to decode JSON using custom decoder

type DefaultDecoder added in v1.6.0

type DefaultDecoder struct{}

DefaultDecoder use json.Unmarshal to decode JSON

func (*DefaultDecoder) Decode added in v1.6.0

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 New

func New(options ...OptionFunc) *JSONQ

New returns a new instance of JSONQ

func (*JSONQ) Avg

func (j *JSONQ) Avg(property ...string) float64

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

func (*JSONQ) Copy

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

func (j *JSONQ) Count() int

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

func (*JSONQ) Distinct added in v1.8.0

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

Distinct builds distinct value using provided attribute/column/property

func (*JSONQ) Error

func (j *JSONQ) Error() error

Error returns first occurred error

func (*JSONQ) Errors

func (j *JSONQ) Errors() []error

Errors returns list of all errors

func (*JSONQ) File

func (j *JSONQ) File(filename string) *JSONQ

File read the json content from physical file

func (*JSONQ) Find

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

Find returns the result of a exact matching path

func (*JSONQ) First

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

First returns the first element of a list

func (*JSONQ) From

func (j *JSONQ) From(node string) *JSONQ

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

func (*JSONQ) Get

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

Get return the result

func (*JSONQ) GroupBy

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 (*JSONQ) JSONString

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

JSONString reads the json content from valid json string

func (*JSONQ) Last

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

Last returns the last element of a list

func (*JSONQ) Limit added in v1.4.0

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

Limit limits the number of records in result

func (*JSONQ) Macro

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

Macro adds a new query func to the JSONQ

func (*JSONQ) Max

func (j *JSONQ) Max(property ...string) float64

Max returns maximum value from array or from map using property

func (*JSONQ) Min

func (j *JSONQ) Min(property ...string) float64

Min returns minimum value from array or from map using property

func (*JSONQ) Nth

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

Nth returns the nth element of a list

func (*JSONQ) Only

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

Only collects the properties from a list of object

func (*JSONQ) OrWhere

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

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

func (*JSONQ) Out added in v1.9.0

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

Out write the queried data to defined custom type

func (*JSONQ) Pluck

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

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

func (*JSONQ) Reader

func (j *JSONQ) Reader(r io.Reader) *JSONQ

Reader reads the json content from io reader

func (*JSONQ) Reset

func (j *JSONQ) Reset() *JSONQ

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

func (*JSONQ) Select added in v1.1.0

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

Select use for selection of the properties from query result

func (*JSONQ) Sort

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

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

func (*JSONQ) SortBy

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

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

func (*JSONQ) String

func (j *JSONQ) String() string

String satisfies stringer interface

func (*JSONQ) Sum

func (j *JSONQ) Sum(property ...string) float64

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

func (*JSONQ) Where

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

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

func (*JSONQ) WhereContains

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

WhereContains satisfies Where clause which contains provided value(string)

func (*JSONQ) WhereEndsWith

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

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

func (*JSONQ) WhereEqual

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

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

func (*JSONQ) WhereIn

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

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

func (*JSONQ) WhereLenEqual added in v1.7.0

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

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

func (*JSONQ) WhereLenNotEqual added in v1.7.0

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

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

func (*JSONQ) WhereNil

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

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

func (*JSONQ) WhereNotEqual

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

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

func (*JSONQ) WhereNotIn

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

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

func (*JSONQ) WhereNotNil

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

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

func (*JSONQ) WhereStartsWith

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

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

func (*JSONQ) WhereStrictContains

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

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

type OptionFunc added in v1.6.0

type OptionFunc func(*JSONQ) error

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

func SetDecoder added in v1.6.0

func SetDecoder(u Decoder) OptionFunc

SetDecoder take a custom decoder to decode JSON

type QueryFunc

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

QueryFunc describes a conditional function which perform comparison

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL